2014-01-28 16:25:10 -05:00
|
|
|
|
using MediaBrowser.Common.ScheduledTasks;
|
2013-03-04 00:43:06 -05:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-08-28 15:56:06 -04:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-03-20 17:17:47 -04:00
|
|
|
|
using MediaBrowser.Server.Implementations.ScheduledTasks;
|
2013-09-19 11:13:34 -04:00
|
|
|
|
using Microsoft.Win32;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-03-08 00:08:27 -05:00
|
|
|
|
namespace MediaBrowser.Server.Implementations.IO
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
public class LibraryMonitor : ILibraryMonitor
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The file system watchers
|
|
|
|
|
/// </summary>
|
2013-09-19 11:13:34 -04:00
|
|
|
|
private readonly ConcurrentDictionary<string, FileSystemWatcher> _fileSystemWatchers = new ConcurrentDictionary<string, FileSystemWatcher>(StringComparer.OrdinalIgnoreCase);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The update timer
|
|
|
|
|
/// </summary>
|
2013-03-08 00:08:27 -05:00
|
|
|
|
private Timer _updateTimer;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The affected paths
|
|
|
|
|
/// </summary>
|
2013-03-08 00:08:27 -05:00
|
|
|
|
private readonly ConcurrentDictionary<string, string> _affectedPaths = new ConcurrentDictionary<string, string>();
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A dynamic list of paths that should be ignored. Added to during our own file sytem modifications.
|
|
|
|
|
/// </summary>
|
2013-04-28 09:24:20 -04:00
|
|
|
|
private readonly ConcurrentDictionary<string, string> _tempIgnoredPaths = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-04-13 17:45:29 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Any file name ending in any of these will be ignored by the watchers
|
|
|
|
|
/// </summary>
|
2013-06-09 09:31:37 -04:00
|
|
|
|
private readonly IReadOnlyList<string> _alwaysIgnoreFiles = new List<string> { "thumbs.db", "small.jpg", "albumart.jpg" };
|
2013-04-13 17:45:29 -04:00
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The timer lock
|
|
|
|
|
/// </summary>
|
2013-03-08 00:08:27 -05:00
|
|
|
|
private readonly object _timerLock = new object();
|
2013-02-20 20:33:05 -05: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 16:25:10 -05:00
|
|
|
|
private void TemporarilyIgnore(string path)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2013-03-08 00:08:27 -05:00
|
|
|
|
_tempIgnoredPaths[path] = path;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes the temp ignore.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
2014-01-28 16:25:10 -05:00
|
|
|
|
private async void RemoveTempIgnore(string path)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2013-07-05 12:47:10 -04:00
|
|
|
|
// This is an arbitraty amount of time, but delay it because file system writes often trigger events after RemoveTempIgnore has been called.
|
2013-11-08 10:35:11 -05:00
|
|
|
|
// Seeing long delays in some situations, especially over the network.
|
2013-11-23 15:01:22 -05:00
|
|
|
|
// Seeing delays up to 40 seconds, but not going to ignore changes for that long.
|
2013-11-26 21:38:11 -05:00
|
|
|
|
await Task.Delay(1500).ConfigureAwait(false);
|
2013-07-05 12:47:10 -04:00
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
string val;
|
2013-03-08 00:08:27 -05:00
|
|
|
|
_tempIgnoredPaths.TryRemove(path, out val);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 16:25:10 -05:00
|
|
|
|
public void ReportFileSystemChangeBeginning(string path)
|
|
|
|
|
{
|
|
|
|
|
TemporarilyIgnore(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReportFileSystemChangeComplete(string path, bool refreshPath)
|
|
|
|
|
{
|
|
|
|
|
RemoveTempIgnore(path);
|
|
|
|
|
|
|
|
|
|
if (refreshPath)
|
|
|
|
|
{
|
|
|
|
|
ReportFileSystemChanged(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the logger.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The logger.</value>
|
|
|
|
|
private ILogger Logger { get; set; }
|
|
|
|
|
|
2013-02-23 02:57:11 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the task manager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The task manager.</value>
|
|
|
|
|
private ITaskManager TaskManager { get; set; }
|
2013-02-28 14:32:41 -05:00
|
|
|
|
|
|
|
|
|
private ILibraryManager LibraryManager { get; set; }
|
2013-03-04 00:43:06 -05:00
|
|
|
|
private IServerConfigurationManager ConfigurationManager { get; set; }
|
2013-02-28 14:32:41 -05:00
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// <summary>
|
2014-01-28 16:25:10 -05:00
|
|
|
|
/// Initializes a new instance of the <see cref="LibraryMonitor" /> class.
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// </summary>
|
2014-01-28 16:25:10 -05:00
|
|
|
|
public LibraryMonitor(ILogManager logManager, ITaskManager taskManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2013-02-23 02:57:11 -05:00
|
|
|
|
if (taskManager == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("taskManager");
|
|
|
|
|
}
|
2013-02-21 15:26:35 -05:00
|
|
|
|
|
2013-02-28 14:32:41 -05:00
|
|
|
|
LibraryManager = libraryManager;
|
2013-02-23 02:57:11 -05:00
|
|
|
|
TaskManager = taskManager;
|
2014-01-28 16:25:10 -05:00
|
|
|
|
Logger = logManager.GetLogger(GetType().Name);
|
2013-03-04 00:43:06 -05:00
|
|
|
|
ConfigurationManager = configurationManager;
|
2013-09-19 11:13:34 -04:00
|
|
|
|
|
|
|
|
|
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the PowerModeChanged event of the SystemEvents control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="PowerModeChangedEventArgs"/> instance containing the event data.</param>
|
|
|
|
|
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Stop();
|
|
|
|
|
Start();
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
2013-04-28 09:24:20 -04:00
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts this instance.
|
|
|
|
|
/// </summary>
|
2013-03-08 00:08:27 -05:00
|
|
|
|
public void Start()
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2013-05-03 00:10:11 -04:00
|
|
|
|
LibraryManager.ItemAdded += LibraryManager_ItemAdded;
|
|
|
|
|
LibraryManager.ItemRemoved += LibraryManager_ItemRemoved;
|
2013-02-28 14:32:41 -05:00
|
|
|
|
|
|
|
|
|
var pathsToWatch = new List<string> { LibraryManager.RootFolder.Path };
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-08-28 15:56:06 -04:00
|
|
|
|
var paths = LibraryManager
|
|
|
|
|
.RootFolder
|
|
|
|
|
.Children
|
|
|
|
|
.OfType<Folder>()
|
|
|
|
|
.Where(i => i.LocationType != LocationType.Remote && i.LocationType != LocationType.Virtual)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
.SelectMany(f =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Accessing ResolveArgs could involve file system access
|
|
|
|
|
return f.ResolveArgs.PhysicalLocations;
|
|
|
|
|
}
|
|
|
|
|
catch (IOException)
|
|
|
|
|
{
|
2013-04-28 09:24:20 -04:00
|
|
|
|
return new string[] { };
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
2013-08-28 15:56:06 -04:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.OrderBy(i => i)
|
|
|
|
|
.ToList();
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
foreach (var path in paths)
|
|
|
|
|
{
|
|
|
|
|
if (!ContainsParentFolder(pathsToWatch, path))
|
|
|
|
|
{
|
|
|
|
|
pathsToWatch.Add(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var path in pathsToWatch)
|
|
|
|
|
{
|
|
|
|
|
StartWatchingPath(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 00:10:11 -04: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)
|
|
|
|
|
{
|
|
|
|
|
if (e.Item.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>
|
|
|
|
|
void LibraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Item.Parent is AggregateFolder)
|
|
|
|
|
{
|
|
|
|
|
StartWatchingPath(e.Item.Path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 20:33:05 -05: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)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
// 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(() =>
|
|
|
|
|
{
|
|
|
|
|
var newWatcher = new FileSystemWatcher(path, "*") { IncludeSubdirectories = true, InternalBufferSize = 32767 };
|
|
|
|
|
|
|
|
|
|
newWatcher.Created += watcher_Changed;
|
|
|
|
|
newWatcher.Deleted += watcher_Changed;
|
|
|
|
|
newWatcher.Renamed += watcher_Changed;
|
|
|
|
|
newWatcher.Changed += watcher_Changed;
|
|
|
|
|
|
|
|
|
|
newWatcher.Error += watcher_Error;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-05-05 09:40:44 -04:00
|
|
|
|
if (_fileSystemWatchers.TryAdd(path, newWatcher))
|
|
|
|
|
{
|
|
|
|
|
newWatcher.EnableRaisingEvents = true;
|
|
|
|
|
Logger.Info("Watching directory " + path);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Unable to add directory watcher for {0}. It already exists in the dictionary." + path);
|
|
|
|
|
newWatcher.Dispose();
|
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error watching path: {0}", ex, path);
|
|
|
|
|
}
|
|
|
|
|
catch (PlatformNotSupportedException ex)
|
|
|
|
|
{
|
|
|
|
|
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 09:40:44 -04:00
|
|
|
|
FileSystemWatcher watcher;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-05-05 09:40:44 -04:00
|
|
|
|
if (_fileSystemWatchers.TryGetValue(path, out watcher))
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
DisposeWatcher(watcher);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the watcher.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="watcher">The watcher.</param>
|
|
|
|
|
private void DisposeWatcher(FileSystemWatcher watcher)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Stopping directory watching for path {0}", watcher.Path);
|
|
|
|
|
|
|
|
|
|
watcher.EnableRaisingEvents = false;
|
|
|
|
|
watcher.Dispose();
|
|
|
|
|
|
2013-05-05 09:40:44 -04:00
|
|
|
|
RemoveWatcherFromList(watcher);
|
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-05-05 09:40:44 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes the watcher from list.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="watcher">The watcher.</param>
|
|
|
|
|
private void RemoveWatcherFromList(FileSystemWatcher watcher)
|
|
|
|
|
{
|
|
|
|
|
FileSystemWatcher removed;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-05-05 09:40:44 -04:00
|
|
|
|
_fileSystemWatchers.TryRemove(watcher.Path, out removed);
|
2013-02-20 20:33:05 -05: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 11:13:34 -04:00
|
|
|
|
void watcher_Error(object sender, ErrorEventArgs e)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
var ex = e.GetException();
|
2013-04-28 09:24:20 -04:00
|
|
|
|
var dw = (FileSystemWatcher)sender;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-04-28 09:24:20 -04:00
|
|
|
|
Logger.ErrorException("Error in Directory watcher for: " + dw.Path, ex);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-09-19 11:13:34 -04:00
|
|
|
|
DisposeWatcher(dw);
|
2013-02-20 20:33:05 -05: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 10:03:23 -04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OnWatcherChanged(e);
|
|
|
|
|
}
|
2014-01-28 16:25:10 -05:00
|
|
|
|
catch (Exception ex)
|
2013-10-31 10:03:23 -04:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
Logger.ErrorException("Exception in watcher changed. Path: {0}", ex, e.FullPath);
|
2013-10-31 10:03:23 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnWatcherChanged(FileSystemEventArgs e)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
Logger.Debug("Watcher sees change of type " + e.ChangeType + " to " + e.FullPath);
|
2013-06-09 09:31:37 -04:00
|
|
|
|
|
2014-01-28 16:25:10 -05:00
|
|
|
|
ReportFileSystemChanged(e.FullPath);
|
|
|
|
|
}
|
2013-06-09 09:31:37 -04:00
|
|
|
|
|
2014-01-28 16:25:10 -05:00
|
|
|
|
public void ReportFileSystemChanged(string path)
|
|
|
|
|
{
|
|
|
|
|
var filename = Path.GetFileName(path);
|
2013-07-29 11:38:11 -04:00
|
|
|
|
|
2014-01-28 16:25:10 -05:00
|
|
|
|
// Ignore certain files
|
|
|
|
|
if (!string.IsNullOrEmpty(filename) && _alwaysIgnoreFiles.Contains(filename, StringComparer.OrdinalIgnoreCase))
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-04-13 17:45:29 -04:00
|
|
|
|
|
2013-05-19 19:44:05 -04:00
|
|
|
|
var tempIgnorePaths = _tempIgnoredPaths.Keys.ToList();
|
|
|
|
|
|
2013-11-04 11:20:44 -05:00
|
|
|
|
// If the parent of an ignored path has a change event, ignore that too
|
|
|
|
|
if (tempIgnorePaths.Any(i =>
|
2013-05-19 19:44:05 -04:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
if (string.Equals(i, path, StringComparison.OrdinalIgnoreCase))
|
2013-11-02 15:30:29 -04:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
Logger.Debug("Ignoring change to {0}", path);
|
2013-11-04 11:20:44 -05:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-02 15:30:29 -04:00
|
|
|
|
|
2013-11-04 11:20:44 -05:00
|
|
|
|
// Go up a level
|
|
|
|
|
var parent = Path.GetDirectoryName(i);
|
2014-01-28 16:25:10 -05:00
|
|
|
|
if (string.Equals(parent, path, StringComparison.OrdinalIgnoreCase))
|
2013-11-04 11:20:44 -05:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
Logger.Debug("Ignoring change to {0}", path);
|
2013-11-04 11:20:44 -05:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Go up another level
|
|
|
|
|
if (!string.IsNullOrEmpty(parent))
|
|
|
|
|
{
|
|
|
|
|
parent = Path.GetDirectoryName(i);
|
2014-01-28 16:25:10 -05:00
|
|
|
|
if (string.Equals(parent, path, StringComparison.OrdinalIgnoreCase))
|
2013-11-02 15:30:29 -04:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
Logger.Debug("Ignoring change to {0}", path);
|
2013-11-02 15:30:29 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-04 11:20:44 -05:00
|
|
|
|
}
|
2013-11-02 15:30:29 -04:00
|
|
|
|
|
2014-01-28 16:25:10 -05:00
|
|
|
|
if (i.StartsWith(path, StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
path.StartsWith(i, StringComparison.OrdinalIgnoreCase))
|
2013-11-07 10:57:12 -05:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
Logger.Debug("Ignoring change to {0}", path);
|
2013-11-07 10:57:12 -05:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-04 11:20:44 -05:00
|
|
|
|
return false;
|
2013-11-02 15:30:29 -04:00
|
|
|
|
|
2013-11-04 11:20:44 -05:00
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
return;
|
2013-05-19 19:44:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 16:25:10 -05:00
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var affectedPath = path;
|
|
|
|
|
_affectedPaths.AddOrUpdate(path, path, (key, oldValue) => affectedPath);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-03-08 00:08:27 -05:00
|
|
|
|
lock (_timerLock)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2013-03-08 00:08:27 -05:00
|
|
|
|
if (_updateTimer == null)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
_updateTimer = new Timer(TimerStopped, null, TimeSpan.FromSeconds(ConfigurationManager.Configuration.RealtimeWatcherDelay), TimeSpan.FromMilliseconds(-1));
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
_updateTimer.Change(TimeSpan.FromSeconds(ConfigurationManager.Configuration.RealtimeWatcherDelay), TimeSpan.FromMilliseconds(-1));
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Timers the stopped.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="stateInfo">The state info.</param>
|
|
|
|
|
private async void TimerStopped(object stateInfo)
|
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
Logger.Debug("Timer stopped.");
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2014-01-28 16:25:10 -05:00
|
|
|
|
DisposeTimer();
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-03-08 00:08:27 -05:00
|
|
|
|
var paths = _affectedPaths.Keys.ToList();
|
|
|
|
|
_affectedPaths.Clear();
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
await ProcessPathChanges(paths).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 16:25:10 -05:00
|
|
|
|
private void DisposeTimer()
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
lock (_timerLock)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
if (_updateTimer != null)
|
2013-05-07 14:57:27 -04:00
|
|
|
|
{
|
2014-01-28 16:25:10 -05:00
|
|
|
|
_updateTimer.Dispose();
|
|
|
|
|
_updateTimer = null;
|
2013-05-07 14:57:27 -04:00
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes the path changes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="paths">The paths.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private async Task ProcessPathChanges(List<string> paths)
|
|
|
|
|
{
|
|
|
|
|
var itemsToRefresh = paths.Select(Path.GetDirectoryName)
|
|
|
|
|
.Select(GetAffectedBaseItem)
|
|
|
|
|
.Where(item => item != null)
|
|
|
|
|
.Distinct()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var p in paths) Logger.Info(p + " reports change.");
|
|
|
|
|
|
|
|
|
|
// If the root folder changed, run the library task so the user can see it
|
|
|
|
|
if (itemsToRefresh.Any(i => i is AggregateFolder))
|
|
|
|
|
{
|
2013-02-23 02:57:11 -05:00
|
|
|
|
TaskManager.CancelIfRunningAndQueue<RefreshMediaLibraryTask>();
|
2013-02-20 20:33:05 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-29 21:41:22 -05:00
|
|
|
|
foreach (var item in itemsToRefresh)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2013-12-29 21:41:22 -05:00
|
|
|
|
Logger.Info(item.Name + " (" + item.Path + ") will be refreshed.");
|
2013-04-28 09:24:20 -04:00
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-12-29 21:41:22 -05:00
|
|
|
|
await item.ChangedExternally().ConfigureAwait(false);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
// For now swallow and log.
|
|
|
|
|
// Research item: If an IOException occurs, the item may be in a disconnected state (media unavailable)
|
|
|
|
|
// Should we remove it from it's parent?
|
2013-12-29 21:41:22 -05:00
|
|
|
|
Logger.ErrorException("Error refreshing {0}", ex, item.Name);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
2013-03-27 18:13:46 -04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-12-29 21:41:22 -05:00
|
|
|
|
Logger.ErrorException("Error refreshing {0}", ex, item.Name);
|
2013-03-27 18:13:46 -04:00
|
|
|
|
}
|
2013-12-29 21:41:22 -05:00
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the affected base item.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns>BaseItem.</returns>
|
|
|
|
|
private BaseItem GetAffectedBaseItem(string path)
|
|
|
|
|
{
|
|
|
|
|
BaseItem item = null;
|
|
|
|
|
|
|
|
|
|
while (item == null && !string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
2013-02-28 14:32:41 -05:00
|
|
|
|
item = LibraryManager.RootFolder.FindByPath(path);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
path = Path.GetDirectoryName(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
|
|
|
|
// If the item has been deleted find the first valid parent that still exists
|
|
|
|
|
while (!Directory.Exists(item.Path) && !File.Exists(item.Path))
|
|
|
|
|
{
|
|
|
|
|
item = item.Parent;
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops this instance.
|
|
|
|
|
/// </summary>
|
2013-03-08 00:08:27 -05:00
|
|
|
|
public void Stop()
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2013-05-03 00:10:11 -04:00
|
|
|
|
LibraryManager.ItemAdded -= LibraryManager_ItemAdded;
|
|
|
|
|
LibraryManager.ItemRemoved -= LibraryManager_ItemRemoved;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-05-05 09:40:44 -04:00
|
|
|
|
foreach (var watcher in _fileSystemWatchers.Values.ToList())
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
watcher.Changed -= watcher_Changed;
|
|
|
|
|
watcher.EnableRaisingEvents = false;
|
|
|
|
|
watcher.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 16:25:10 -05:00
|
|
|
|
DisposeTimer();
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-08-13 10:41:45 -04:00
|
|
|
|
_fileSystemWatchers.Clear();
|
2013-03-08 00:08:27 -05:00
|
|
|
|
_affectedPaths.Clear();
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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="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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|