2016-11-11 04:25:21 +00:00
|
|
|
|
using System;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-11-11 04:25:21 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
2016-10-25 19:02:04 +00:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-11-11 04:25:21 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2016-11-04 18:56:47 +00:00
|
|
|
|
using MediaBrowser.Model.System;
|
2016-10-23 19:47:34 +00:00
|
|
|
|
using MediaBrowser.Model.Tasks;
|
2016-11-03 06:37:52 +00:00
|
|
|
|
using MediaBrowser.Model.Threading;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
|
namespace Emby.Server.Implementations.IO
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2014-01-28 21:25:10 +00:00
|
|
|
|
public class LibraryMonitor : ILibraryMonitor
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The file system watchers
|
|
|
|
|
/// </summary>
|
2013-09-19 15:13:34 +00:00
|
|
|
|
private readonly ConcurrentDictionary<string, FileSystemWatcher> _fileSystemWatchers = new ConcurrentDictionary<string, FileSystemWatcher>(StringComparer.OrdinalIgnoreCase);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The affected paths
|
|
|
|
|
/// </summary>
|
2016-05-26 20:54:05 +00:00
|
|
|
|
private readonly List<FileRefresher> _activeRefreshers = new List<FileRefresher>();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A dynamic list of paths that should be ignored. Added to during our own file sytem modifications.
|
|
|
|
|
/// </summary>
|
2013-04-28 13:24:20 +00:00
|
|
|
|
private readonly ConcurrentDictionary<string, string> _tempIgnoredPaths = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-04-13 21:45:29 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Any file name ending in any of these will be ignored by the watchers
|
|
|
|
|
/// </summary>
|
2017-08-19 19:43:35 +00:00
|
|
|
|
private readonly string[] _alwaysIgnoreFiles = new string[]
|
2014-02-26 04:38:21 +00:00
|
|
|
|
{
|
2016-05-26 20:54:05 +00:00
|
|
|
|
"small.jpg",
|
2014-02-26 04:38:21 +00:00
|
|
|
|
"albumart.jpg",
|
|
|
|
|
|
|
|
|
|
// WMC temp recording directories that will constantly be written to
|
|
|
|
|
"TempRec",
|
2016-09-25 18:39:13 +00:00
|
|
|
|
"TempSBE"
|
2014-02-26 04:38:21 +00:00
|
|
|
|
};
|
2013-04-13 21:45:29 +00:00
|
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
|
private readonly string[] _alwaysIgnoreSubstrings = new string[]
|
2016-09-07 03:38:01 +00:00
|
|
|
|
{
|
|
|
|
|
// Synology
|
2016-09-25 18:39:13 +00:00
|
|
|
|
"eaDir",
|
|
|
|
|
"#recycle",
|
2016-09-07 03:38:01 +00:00
|
|
|
|
".wd_tv",
|
|
|
|
|
".actors"
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
|
private readonly string[] _alwaysIgnoreExtensions = new string[]
|
2016-08-16 17:08:37 +00:00
|
|
|
|
{
|
|
|
|
|
// thumbs.db
|
|
|
|
|
".db",
|
|
|
|
|
|
|
|
|
|
// bts sync files
|
2016-08-17 05:29:05 +00:00
|
|
|
|
".bts",
|
|
|
|
|
".sync"
|
2016-08-16 17:08:37 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-02-21 01:33:05 +00: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 21:25:10 +00:00
|
|
|
|
private void TemporarilyIgnore(string path)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-03-08 05:08:27 +00:00
|
|
|
|
_tempIgnoredPaths[path] = path;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 21:25:10 +00:00
|
|
|
|
public void ReportFileSystemChangeBeginning(string path)
|
|
|
|
|
{
|
2014-01-28 21:51:47 +00:00
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 21:25:10 +00:00
|
|
|
|
TemporarilyIgnore(path);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-24 18:45:20 +00:00
|
|
|
|
public bool IsPathLocked(string path)
|
|
|
|
|
{
|
2017-08-23 16:45:58 +00:00
|
|
|
|
// This method is not used by the core but it used by auto-organize
|
|
|
|
|
|
2016-02-24 18:45:20 +00:00
|
|
|
|
var lockedPaths = _tempIgnoredPaths.Keys.ToList();
|
2017-01-11 17:56:26 +00:00
|
|
|
|
return lockedPaths.Any(i => _fileSystem.AreEqual(i, path) || _fileSystem.ContainsSubPath(i, path));
|
2016-02-24 18:45:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
public async void ReportFileSystemChangeComplete(string path, bool refreshPath)
|
2014-01-28 21:25:10 +00:00
|
|
|
|
{
|
2014-01-28 21:51:47 +00:00
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-03 05:30:14 +00:00
|
|
|
|
// This is an arbitraty amount of time, but delay it because file system writes often trigger events long after the file was actually written to.
|
2015-05-24 18:33:28 +00:00
|
|
|
|
// Seeing long delays in some situations, especially over the network, sometimes up to 45 seconds
|
2015-06-03 05:30:14 +00: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-22 05:17:09 +00:00
|
|
|
|
await Task.Delay(45000).ConfigureAwait(false);
|
2014-01-29 05:17:58 +00:00
|
|
|
|
|
|
|
|
|
string val;
|
|
|
|
|
_tempIgnoredPaths.TryRemove(path, out val);
|
2014-01-28 21:25:10 +00:00
|
|
|
|
|
|
|
|
|
if (refreshPath)
|
|
|
|
|
{
|
2016-09-15 20:30:46 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ReportFileSystemChanged(path);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error in ReportFileSystemChanged for {0}", ex, path);
|
|
|
|
|
}
|
2014-01-28 21:25:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the logger.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The logger.</value>
|
|
|
|
|
private ILogger Logger { get; set; }
|
|
|
|
|
|
2013-02-23 07:57:11 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the task manager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The task manager.</value>
|
|
|
|
|
private ITaskManager TaskManager { get; set; }
|
2013-02-28 19:32:41 +00:00
|
|
|
|
|
|
|
|
|
private ILibraryManager LibraryManager { get; set; }
|
2013-03-04 05:43:06 +00:00
|
|
|
|
private IServerConfigurationManager ConfigurationManager { get; set; }
|
2013-02-28 19:32:41 +00:00
|
|
|
|
|
2014-01-29 01:46:04 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2016-11-03 06:37:52 +00:00
|
|
|
|
private readonly ITimerFactory _timerFactory;
|
2016-11-04 19:51:59 +00:00
|
|
|
|
private readonly IEnvironmentInfo _environmentInfo;
|
2014-01-28 21:51:47 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
2014-01-28 21:25:10 +00:00
|
|
|
|
/// Initializes a new instance of the <see cref="LibraryMonitor" /> class.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2016-11-04 19:51:59 +00:00
|
|
|
|
public LibraryMonitor(ILogManager logManager, ITaskManager taskManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ITimerFactory timerFactory, ISystemEvents systemEvents, IEnvironmentInfo environmentInfo)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-23 07:57:11 +00:00
|
|
|
|
if (taskManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("taskManager");
|
|
|
|
|
}
|
2013-02-21 20:26:35 +00:00
|
|
|
|
|
2013-02-28 19:32:41 +00:00
|
|
|
|
LibraryManager = libraryManager;
|
2013-02-23 07:57:11 +00:00
|
|
|
|
TaskManager = taskManager;
|
2014-01-28 21:25:10 +00:00
|
|
|
|
Logger = logManager.GetLogger(GetType().Name);
|
2013-03-04 05:43:06 +00:00
|
|
|
|
ConfigurationManager = configurationManager;
|
2014-01-29 01:46:04 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2016-11-03 06:37:52 +00:00
|
|
|
|
_timerFactory = timerFactory;
|
2016-11-04 19:51:59 +00:00
|
|
|
|
_environmentInfo = environmentInfo;
|
2013-09-19 15:13:34 +00:00
|
|
|
|
|
2016-11-04 18:56:47 +00:00
|
|
|
|
systemEvents.Resume += _systemEvents_Resume;
|
2013-09-19 15:13:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 18:56:47 +00:00
|
|
|
|
private void _systemEvents_Resume(object sender, EventArgs e)
|
2014-01-29 20:30:26 +00:00
|
|
|
|
{
|
|
|
|
|
Restart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Restart()
|
2013-09-19 15:13:34 +00:00
|
|
|
|
{
|
|
|
|
|
Stop();
|
2017-04-02 00:36:06 +00:00
|
|
|
|
|
|
|
|
|
if (!_disposed)
|
|
|
|
|
{
|
|
|
|
|
Start();
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2013-04-28 13:24:20 +00:00
|
|
|
|
|
2016-08-24 20:46:26 +00:00
|
|
|
|
private bool IsLibraryMonitorEnabaled(BaseItem item)
|
2014-01-29 20:30:26 +00:00
|
|
|
|
{
|
2016-10-23 20:04:31 +00:00
|
|
|
|
if (item is BasePluginFolder)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-24 20:46:26 +00:00
|
|
|
|
var options = LibraryManager.GetLibraryOptions(item);
|
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
if (options != null)
|
2014-01-29 20:30:26 +00:00
|
|
|
|
{
|
2016-08-24 20:46:26 +00:00
|
|
|
|
return options.EnableRealtimeMonitor;
|
2014-01-29 20:30:26 +00:00
|
|
|
|
}
|
2016-08-24 20:46:26 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
return false;
|
2014-01-29 20:30:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-24 20:46:26 +00:00
|
|
|
|
public void Start()
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-03 04:10:11 +00:00
|
|
|
|
LibraryManager.ItemAdded += LibraryManager_ItemAdded;
|
|
|
|
|
LibraryManager.ItemRemoved += LibraryManager_ItemRemoved;
|
2013-02-28 19:32:41 +00:00
|
|
|
|
|
2016-08-24 20:46:26 +00:00
|
|
|
|
var pathsToWatch = new List<string> { };
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-08-28 19:56:06 +00:00
|
|
|
|
var paths = LibraryManager
|
|
|
|
|
.RootFolder
|
|
|
|
|
.Children
|
2016-08-24 20:46:26 +00:00
|
|
|
|
.Where(IsLibraryMonitorEnabaled)
|
2013-08-28 19:56:06 +00:00
|
|
|
|
.OfType<Folder>()
|
2014-02-06 04:39:16 +00:00
|
|
|
|
.SelectMany(f => f.PhysicalLocations)
|
2013-08-28 19:56:06 +00:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.OrderBy(i => i)
|
|
|
|
|
.ToList();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
foreach (var path in paths)
|
|
|
|
|
{
|
|
|
|
|
if (!ContainsParentFolder(pathsToWatch, path))
|
|
|
|
|
{
|
|
|
|
|
pathsToWatch.Add(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var path in pathsToWatch)
|
|
|
|
|
{
|
|
|
|
|
StartWatchingPath(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-24 20:46:26 +00:00
|
|
|
|
private void StartWatching(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
if (IsLibraryMonitorEnabaled(item))
|
|
|
|
|
{
|
|
|
|
|
StartWatchingPath(item.Path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 04:10:11 +00: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>
|
|
|
|
|
void LibraryManager_ItemRemoved(object sender, ItemChangeEventArgs e)
|
|
|
|
|
{
|
2015-11-11 14:56:31 +00:00
|
|
|
|
if (e.Item.GetParent() is AggregateFolder)
|
2013-05-03 04:10:11 +00: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>
|
|
|
|
|
void LibraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
|
|
|
|
|
{
|
2015-11-11 14:56:31 +00:00
|
|
|
|
if (e.Item.GetParent() is AggregateFolder)
|
2013-05-03 04:10:11 +00:00
|
|
|
|
{
|
2016-08-24 20:46:26 +00:00
|
|
|
|
StartWatching(e.Item);
|
2013-05-03 04:10:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00: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>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">path</exception>
|
|
|
|
|
private static bool ContainsParentFolder(IEnumerable<string> lst, string path)
|
|
|
|
|
{
|
2016-04-09 02:30:22 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("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);
|
|
|
|
|
|
2016-03-27 21:11:27 +00:00
|
|
|
|
return path.Equals(compare, StringComparison.OrdinalIgnoreCase) || (path.StartsWith(compare, StringComparison.OrdinalIgnoreCase) && path[compare.Length] == Path.DirectorySeparatorChar);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the watching path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
private void StartWatchingPath(string path)
|
|
|
|
|
{
|
2017-02-03 20:52:56 +00:00
|
|
|
|
if (!_fileSystem.DirectoryExists(path))
|
|
|
|
|
{
|
|
|
|
|
// Seeing a crash in the mono runtime due to an exception being thrown on a different thread
|
|
|
|
|
Logger.Info("Skipping realtime monitor for {0} because the path does not exist", path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 18:05:47 +00:00
|
|
|
|
if (_environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows)
|
|
|
|
|
{
|
|
|
|
|
if (path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase) || path.StartsWith("smb://", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
// not supported
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 20:52:56 +00:00
|
|
|
|
// Already being watched
|
|
|
|
|
if (_fileSystemWatchers.ContainsKey(path))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00: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 19:30:21 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var newWatcher = new FileSystemWatcher(path, "*")
|
|
|
|
|
{
|
2016-01-06 22:47:32 +00:00
|
|
|
|
IncludeSubdirectories = true
|
2014-03-27 19:30:21 +00:00
|
|
|
|
};
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2016-11-11 07:30:03 +00:00
|
|
|
|
if (_environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows)
|
2016-01-06 22:47:32 +00:00
|
|
|
|
{
|
|
|
|
|
newWatcher.InternalBufferSize = 32767;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 02:33:10 +00:00
|
|
|
|
newWatcher.NotifyFilter = NotifyFilters.CreationTime |
|
|
|
|
|
NotifyFilters.DirectoryName |
|
|
|
|
|
NotifyFilters.FileName |
|
|
|
|
|
NotifyFilters.LastWrite |
|
|
|
|
|
NotifyFilters.Size |
|
|
|
|
|
NotifyFilters.Attributes;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2014-03-27 19:30:21 +00:00
|
|
|
|
newWatcher.Created += watcher_Changed;
|
2017-06-06 06:13:49 +00:00
|
|
|
|
|
|
|
|
|
// Seeing mono crashes on background threads we can't catch, testing if this might help
|
|
|
|
|
if (_environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows)
|
|
|
|
|
{
|
|
|
|
|
newWatcher.Deleted += watcher_Changed;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 19:30:21 +00:00
|
|
|
|
newWatcher.Renamed += watcher_Changed;
|
|
|
|
|
newWatcher.Changed += watcher_Changed;
|
|
|
|
|
|
|
|
|
|
newWatcher.Error += watcher_Error;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-05 13:40:44 +00:00
|
|
|
|
if (_fileSystemWatchers.TryAdd(path, newWatcher))
|
|
|
|
|
{
|
|
|
|
|
newWatcher.EnableRaisingEvents = true;
|
|
|
|
|
Logger.Info("Watching directory " + path);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
newWatcher.Dispose();
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
}
|
2014-03-27 19:30:21 +00:00
|
|
|
|
catch (Exception ex)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error watching path: {0}", ex, path);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops the watching path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
private void StopWatchingPath(string path)
|
|
|
|
|
{
|
2013-05-05 13:40:44 +00:00
|
|
|
|
FileSystemWatcher watcher;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-05 13:40:44 +00:00
|
|
|
|
if (_fileSystemWatchers.TryGetValue(path, out watcher))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
DisposeWatcher(watcher);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the watcher.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="watcher">The watcher.</param>
|
|
|
|
|
private void DisposeWatcher(FileSystemWatcher watcher)
|
|
|
|
|
{
|
2014-10-13 20:14:53 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (watcher)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Stopping directory watching for path {0}", watcher.Path);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2014-10-13 20:14:53 +00:00
|
|
|
|
watcher.EnableRaisingEvents = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2015-07-28 19:42:24 +00:00
|
|
|
|
|
2014-10-13 20:14:53 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
RemoveWatcherFromList(watcher);
|
|
|
|
|
}
|
2013-05-05 13:40:44 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-05 13:40:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes the watcher from list.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="watcher">The watcher.</param>
|
|
|
|
|
private void RemoveWatcherFromList(FileSystemWatcher watcher)
|
|
|
|
|
{
|
|
|
|
|
FileSystemWatcher removed;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-05 13:40:44 +00:00
|
|
|
|
_fileSystemWatchers.TryRemove(watcher.Path, out removed);
|
2013-02-21 01:33:05 +00: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>
|
2013-09-19 15:13:34 +00:00
|
|
|
|
void watcher_Error(object sender, ErrorEventArgs e)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
var ex = e.GetException();
|
2013-04-28 13:24:20 +00:00
|
|
|
|
var dw = (FileSystemWatcher)sender;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-04-28 13:24:20 +00:00
|
|
|
|
Logger.ErrorException("Error in Directory watcher for: " + dw.Path, ex);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-09-19 15:13:34 +00:00
|
|
|
|
DisposeWatcher(dw);
|
2013-02-21 01:33:05 +00: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>
|
|
|
|
|
void watcher_Changed(object sender, FileSystemEventArgs e)
|
2013-10-31 14:03:23 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-03-08 19:05:33 +00:00
|
|
|
|
//Logger.Debug("Changed detected of type " + e.ChangeType + " to " + e.FullPath);
|
2014-03-27 19:30:21 +00:00
|
|
|
|
|
2016-09-20 19:43:27 +00:00
|
|
|
|
var path = e.FullPath;
|
|
|
|
|
|
|
|
|
|
ReportFileSystemChanged(path);
|
2013-10-31 14:03:23 +00:00
|
|
|
|
}
|
2014-01-28 21:25:10 +00:00
|
|
|
|
catch (Exception ex)
|
2013-10-31 14:03:23 +00:00
|
|
|
|
{
|
2014-03-29 15:40:32 +00:00
|
|
|
|
Logger.ErrorException("Exception in ReportFileSystemChanged. Path: {0}", ex, e.FullPath);
|
2013-10-31 14:03:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 21:25:10 +00:00
|
|
|
|
public void ReportFileSystemChanged(string path)
|
|
|
|
|
{
|
2014-01-28 21:51:47 +00:00
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
2014-01-29 20:30:26 +00:00
|
|
|
|
|
2014-01-28 21:25:10 +00:00
|
|
|
|
var filename = Path.GetFileName(path);
|
2016-09-07 03:38:01 +00:00
|
|
|
|
|
2016-08-24 20:46:26 +00:00
|
|
|
|
var monitorPath = !string.IsNullOrEmpty(filename) &&
|
|
|
|
|
!_alwaysIgnoreFiles.Contains(filename, StringComparer.OrdinalIgnoreCase) &&
|
2016-09-07 03:38:01 +00:00
|
|
|
|
!_alwaysIgnoreExtensions.Contains(Path.GetExtension(path) ?? string.Empty, StringComparer.OrdinalIgnoreCase) &&
|
|
|
|
|
_alwaysIgnoreSubstrings.All(i => path.IndexOf(i, StringComparison.OrdinalIgnoreCase) == -1);
|
2013-04-13 21:45:29 +00:00
|
|
|
|
|
2014-03-27 19:30:21 +00:00
|
|
|
|
// Ignore certain files
|
2013-05-19 23:44:05 +00:00
|
|
|
|
var tempIgnorePaths = _tempIgnoredPaths.Keys.ToList();
|
|
|
|
|
|
2013-11-04 16:20:44 +00:00
|
|
|
|
// If the parent of an ignored path has a change event, ignore that too
|
|
|
|
|
if (tempIgnorePaths.Any(i =>
|
2013-05-19 23:44:05 +00:00
|
|
|
|
{
|
2017-05-04 07:00:52 +00:00
|
|
|
|
if (_fileSystem.AreEqual(i, path))
|
2013-11-02 19:30:29 +00:00
|
|
|
|
{
|
2014-01-28 21:25:10 +00:00
|
|
|
|
Logger.Debug("Ignoring change to {0}", path);
|
2013-11-04 16:20:44 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-02 19:30:29 +00:00
|
|
|
|
|
2014-01-28 21:51:47 +00:00
|
|
|
|
if (_fileSystem.ContainsSubPath(i, path))
|
2013-11-04 16:20:44 +00:00
|
|
|
|
{
|
2014-01-28 21:25:10 +00:00
|
|
|
|
Logger.Debug("Ignoring change to {0}", path);
|
2013-11-04 16:20:44 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 21:51:47 +00:00
|
|
|
|
// Go up a level
|
2017-05-04 18:14:45 +00:00
|
|
|
|
var parent = _fileSystem.GetDirectoryName(i);
|
2013-11-04 16:20:44 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(parent))
|
|
|
|
|
{
|
2017-05-04 07:00:52 +00:00
|
|
|
|
if (_fileSystem.AreEqual(parent, path))
|
2013-11-02 19:30:29 +00:00
|
|
|
|
{
|
2014-01-28 21:25:10 +00:00
|
|
|
|
Logger.Debug("Ignoring change to {0}", path);
|
2013-11-02 19:30:29 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-07 15:57:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-04 16:20:44 +00:00
|
|
|
|
return false;
|
2013-11-02 19:30:29 +00:00
|
|
|
|
|
2013-11-04 16:20:44 +00:00
|
|
|
|
}))
|
|
|
|
|
{
|
2014-03-27 19:30:21 +00:00
|
|
|
|
monitorPath = false;
|
2013-05-19 23:44:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 19:30:21 +00:00
|
|
|
|
if (monitorPath)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
2016-05-26 20:54:05 +00:00
|
|
|
|
CreateRefresher(path);
|
2014-03-10 02:33:49 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-26 20:54:05 +00:00
|
|
|
|
private void CreateRefresher(string path)
|
2014-04-02 21:55:19 +00:00
|
|
|
|
{
|
2017-05-04 18:14:45 +00:00
|
|
|
|
var parentPath = _fileSystem.GetDirectoryName(path);
|
2016-01-27 18:42:19 +00:00
|
|
|
|
|
2016-05-26 20:54:05 +00:00
|
|
|
|
lock (_activeRefreshers)
|
2014-04-02 21:55:19 +00:00
|
|
|
|
{
|
2016-05-26 20:54:05 +00:00
|
|
|
|
var refreshers = _activeRefreshers.ToList();
|
|
|
|
|
foreach (var refresher in refreshers)
|
2014-04-02 21:55:19 +00:00
|
|
|
|
{
|
2016-05-26 20:54:05 +00:00
|
|
|
|
// Path is already being refreshed
|
2017-05-04 07:00:52 +00:00
|
|
|
|
if (_fileSystem.AreEqual(path, refresher.Path))
|
2016-05-26 20:54:05 +00:00
|
|
|
|
{
|
|
|
|
|
refresher.RestartTimer();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-11-11 04:11:03 +00:00
|
|
|
|
|
2016-05-26 20:54:05 +00:00
|
|
|
|
// Parent folder is already being refreshed
|
|
|
|
|
if (_fileSystem.ContainsSubPath(refresher.Path, path))
|
2014-04-02 21:55:19 +00:00
|
|
|
|
{
|
2016-05-26 20:54:05 +00:00
|
|
|
|
refresher.AddPath(path);
|
|
|
|
|
return;
|
2014-04-02 21:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-26 20:54:05 +00:00
|
|
|
|
// New path is a parent
|
|
|
|
|
if (_fileSystem.ContainsSubPath(path, refresher.Path))
|
|
|
|
|
{
|
|
|
|
|
refresher.ResetPath(path, null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-04-02 21:55:19 +00:00
|
|
|
|
|
2016-05-27 03:46:21 +00:00
|
|
|
|
// They are siblings. Rebase the refresher to the parent folder.
|
2017-05-04 18:14:45 +00:00
|
|
|
|
if (string.Equals(parentPath, _fileSystem.GetDirectoryName(refresher.Path), StringComparison.Ordinal))
|
2016-05-26 20:54:05 +00:00
|
|
|
|
{
|
|
|
|
|
refresher.ResetPath(parentPath, path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-05-07 18:57:27 +00:00
|
|
|
|
}
|
2013-04-28 13:24:20 +00:00
|
|
|
|
|
2017-05-12 05:00:45 +00:00
|
|
|
|
var newRefresher = new FileRefresher(path, _fileSystem, ConfigurationManager, LibraryManager, TaskManager, Logger, _timerFactory, _environmentInfo, LibraryManager);
|
2016-05-26 20:54:05 +00:00
|
|
|
|
newRefresher.Completed += NewRefresher_Completed;
|
|
|
|
|
_activeRefreshers.Add(newRefresher);
|
2013-12-30 02:41:22 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-26 20:54:05 +00:00
|
|
|
|
private void NewRefresher_Completed(object sender, EventArgs e)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2016-05-26 20:54:05 +00:00
|
|
|
|
var refresher = (FileRefresher)sender;
|
|
|
|
|
DisposeRefresher(refresher);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops this instance.
|
|
|
|
|
/// </summary>
|
2013-03-08 05:08:27 +00:00
|
|
|
|
public void Stop()
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-03 04:10:11 +00:00
|
|
|
|
LibraryManager.ItemAdded -= LibraryManager_ItemAdded;
|
|
|
|
|
LibraryManager.ItemRemoved -= LibraryManager_ItemRemoved;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-05 13:40:44 +00:00
|
|
|
|
foreach (var watcher in _fileSystemWatchers.Values.ToList())
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2016-02-26 14:50:44 +00:00
|
|
|
|
watcher.Created -= watcher_Changed;
|
|
|
|
|
watcher.Deleted -= watcher_Changed;
|
|
|
|
|
watcher.Renamed -= watcher_Changed;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
watcher.Changed -= watcher_Changed;
|
2016-02-26 14:50:44 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
watcher.EnableRaisingEvents = false;
|
|
|
|
|
}
|
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
// Seeing this under mono on linux sometimes
|
|
|
|
|
// Collection was modified; enumeration operation may not execute.
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
watcher.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 14:41:45 +00:00
|
|
|
|
_fileSystemWatchers.Clear();
|
2016-05-26 20:54:05 +00: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();
|
|
|
|
|
}
|
|
|
|
|
_activeRefreshers.Clear();
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-02 00:36:06 +00:00
|
|
|
|
private bool _disposed;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2017-04-02 00:36:06 +00:00
|
|
|
|
_disposed = true;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-27 01:31:54 +00:00
|
|
|
|
|
|
|
|
|
public class LibraryMonitorStartup : IServerEntryPoint
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryMonitor _monitor;
|
|
|
|
|
|
|
|
|
|
public LibraryMonitorStartup(ILibraryMonitor monitor)
|
|
|
|
|
{
|
|
|
|
|
_monitor = monitor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
_monitor.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|