diff options
| author | Luke <luke.pulverenti@gmail.com> | 2016-12-18 00:44:33 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-12-18 00:44:33 -0500 |
| commit | e7cebb91a73354dc3e0d0b6340c9fbd6511f4406 (patch) | |
| tree | 6f1c368c766c17b7514fe749c0e92e69cd89194a /Emby.Server.Implementations/IO | |
| parent | 025905a3e4d50b9a2e07fbf4ff0a203af6604ced (diff) | |
| parent | aaa027f3229073e9a40756c3157d41af2a442922 (diff) | |
Merge pull request #2350 from MediaBrowser/beta
Beta
Diffstat (limited to 'Emby.Server.Implementations/IO')
| -rw-r--r-- | Emby.Server.Implementations/IO/FileRefresher.cs | 326 | ||||
| -rw-r--r-- | Emby.Server.Implementations/IO/MbLinkShortcutHandler.cs | 55 | ||||
| -rw-r--r-- | Emby.Server.Implementations/IO/ThrottledStream.cs | 394 |
3 files changed, 775 insertions, 0 deletions
diff --git a/Emby.Server.Implementations/IO/FileRefresher.cs b/Emby.Server.Implementations/IO/FileRefresher.cs new file mode 100644 index 000000000..39033249f --- /dev/null +++ b/Emby.Server.Implementations/IO/FileRefresher.cs @@ -0,0 +1,326 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Model.IO; +using MediaBrowser.Common.Events; +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.IO; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Extensions; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.System; +using MediaBrowser.Model.Tasks; +using MediaBrowser.Model.Threading; + +namespace Emby.Server.Implementations.IO +{ + public class FileRefresher : IDisposable + { + private ILogger Logger { get; set; } + private ITaskManager TaskManager { get; set; } + private ILibraryManager LibraryManager { get; set; } + private IServerConfigurationManager ConfigurationManager { get; set; } + private readonly IFileSystem _fileSystem; + private readonly List<string> _affectedPaths = new List<string>(); + private ITimer _timer; + private readonly ITimerFactory _timerFactory; + private readonly object _timerLock = new object(); + public string Path { get; private set; } + + public event EventHandler<EventArgs> Completed; + private readonly IEnvironmentInfo _environmentInfo; + + public FileRefresher(string path, IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, ITaskManager taskManager, ILogger logger, ITimerFactory timerFactory, IEnvironmentInfo environmentInfo) + { + logger.Debug("New file refresher created for {0}", path); + Path = path; + + _fileSystem = fileSystem; + ConfigurationManager = configurationManager; + LibraryManager = libraryManager; + TaskManager = taskManager; + Logger = logger; + _timerFactory = timerFactory; + _environmentInfo = environmentInfo; + AddPath(path); + } + + private void AddAffectedPath(string path) + { + if (string.IsNullOrWhiteSpace(path)) + { + throw new ArgumentNullException("path"); + } + + if (!_affectedPaths.Contains(path, StringComparer.Ordinal)) + { + _affectedPaths.Add(path); + } + } + + public void AddPath(string path) + { + if (string.IsNullOrWhiteSpace(path)) + { + throw new ArgumentNullException("path"); + } + + lock (_timerLock) + { + AddAffectedPath(path); + } + RestartTimer(); + } + + public void RestartTimer() + { + if (_disposed) + { + return; + } + + lock (_timerLock) + { + if (_disposed) + { + return; + } + + if (_timer == null) + { + _timer = _timerFactory.Create(OnTimerCallback, null, TimeSpan.FromSeconds(ConfigurationManager.Configuration.LibraryMonitorDelay), TimeSpan.FromMilliseconds(-1)); + } + else + { + _timer.Change(TimeSpan.FromSeconds(ConfigurationManager.Configuration.LibraryMonitorDelay), TimeSpan.FromMilliseconds(-1)); + } + } + } + + public void ResetPath(string path, string affectedFile) + { + lock (_timerLock) + { + Logger.Debug("Resetting file refresher from {0} to {1}", Path, path); + + Path = path; + AddAffectedPath(path); + + if (!string.IsNullOrWhiteSpace(affectedFile)) + { + AddAffectedPath(affectedFile); + } + } + RestartTimer(); + } + + private async void OnTimerCallback(object state) + { + List<string> paths; + + lock (_timerLock) + { + paths = _affectedPaths.ToList(); + } + + // Extend the timer as long as any of the paths are still being written to. + if (paths.Any(IsFileLocked)) + { + Logger.Info("Timer extended."); + RestartTimer(); + return; + } + + Logger.Debug("Timer stopped."); + + DisposeTimer(); + EventHelper.FireEventIfNotNull(Completed, this, EventArgs.Empty, Logger); + + try + { + await ProcessPathChanges(paths.ToList()).ConfigureAwait(false); + } + catch (Exception ex) + { + Logger.ErrorException("Error processing directory changes", ex); + } + } + + private async Task ProcessPathChanges(List<string> paths) + { + var itemsToRefresh = paths + .Distinct(StringComparer.OrdinalIgnoreCase) + .Select(GetAffectedBaseItem) + .Where(item => item != null) + .DistinctBy(i => i.Id) + .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)) + { + LibraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None); + return; + } + + foreach (var item in itemsToRefresh) + { + Logger.Info(item.Name + " (" + item.Path + ") will be refreshed."); + + try + { + await item.ChangedExternally().ConfigureAwait(false); + } + 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? + Logger.ErrorException("Error refreshing {0}", ex, item.Name); + } + catch (Exception ex) + { + Logger.ErrorException("Error refreshing {0}", ex, item.Name); + } + } + } + + /// <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)) + { + item = LibraryManager.FindByPath(path, null); + + path = System.IO.Path.GetDirectoryName(path); + } + + if (item != null) + { + // If the item has been deleted find the first valid parent that still exists + while (!_fileSystem.DirectoryExists(item.Path) && !_fileSystem.FileExists(item.Path)) + { + item = item.GetParent(); + + if (item == null) + { + break; + } + } + } + + return item; + } + + private bool IsFileLocked(string path) + { + if (_environmentInfo.OperatingSystem != OperatingSystem.Windows) + { + // Causing lockups on linux + return false; + } + + try + { + var data = _fileSystem.GetFileSystemInfo(path); + + if (!data.Exists + || data.IsDirectory + + // Opening a writable stream will fail with readonly files + || data.IsReadOnly) + { + return false; + } + } + catch (IOException) + { + return false; + } + catch (Exception ex) + { + Logger.ErrorException("Error getting file system info for: {0}", ex, path); + return false; + } + + // In order to determine if the file is being written to, we have to request write access + // But if the server only has readonly access, this is going to cause this entire algorithm to fail + // So we'll take a best guess about our access level + var requestedFileAccess = ConfigurationManager.Configuration.SaveLocalMeta + ? FileAccessMode.ReadWrite + : FileAccessMode.Read; + + try + { + using (_fileSystem.GetFileStream(path, FileOpenMode.Open, requestedFileAccess, FileShareMode.ReadWrite)) + { + //file is not locked + return false; + } + } + //catch (DirectoryNotFoundException) + //{ + // // File may have been deleted + // return false; + //} + catch (FileNotFoundException) + { + // File may have been deleted + return false; + } + catch (UnauthorizedAccessException) + { + Logger.Debug("No write permission for: {0}.", path); + return false; + } + catch (IOException) + { + //the file is unavailable because it is: + //still being written to + //or being processed by another thread + //or does not exist (has already been processed) + Logger.Debug("{0} is locked.", path); + return true; + } + catch (Exception ex) + { + Logger.ErrorException("Error determining if file is locked: {0}", ex, path); + return false; + } + } + + private void DisposeTimer() + { + lock (_timerLock) + { + if (_timer != null) + { + _timer.Dispose(); + _timer = null; + } + } + } + + private bool _disposed; + public void Dispose() + { + _disposed = true; + DisposeTimer(); + } + } +} diff --git a/Emby.Server.Implementations/IO/MbLinkShortcutHandler.cs b/Emby.Server.Implementations/IO/MbLinkShortcutHandler.cs new file mode 100644 index 000000000..0b1391ae0 --- /dev/null +++ b/Emby.Server.Implementations/IO/MbLinkShortcutHandler.cs @@ -0,0 +1,55 @@ +using System; +using System.IO; +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.IO; +using MediaBrowser.Model.IO; + +namespace Emby.Server.Implementations.IO +{ + public class MbLinkShortcutHandler : IShortcutHandler + { + private readonly IFileSystem _fileSystem; + + public MbLinkShortcutHandler(IFileSystem fileSystem) + { + _fileSystem = fileSystem; + } + + public string Extension + { + get { return ".mblink"; } + } + + public string Resolve(string shortcutPath) + { + if (string.IsNullOrEmpty(shortcutPath)) + { + throw new ArgumentNullException("filenshortcutPathame"); + } + + if (string.Equals(Path.GetExtension(shortcutPath), ".mblink", StringComparison.OrdinalIgnoreCase)) + { + var path = _fileSystem.ReadAllText(shortcutPath); + + return _fileSystem.NormalizePath(path); + } + + return null; + } + + public void Create(string shortcutPath, string targetPath) + { + if (string.IsNullOrEmpty(shortcutPath)) + { + throw new ArgumentNullException("shortcutPath"); + } + + if (string.IsNullOrEmpty(targetPath)) + { + throw new ArgumentNullException("targetPath"); + } + + _fileSystem.WriteAllText(shortcutPath, targetPath); + } + } +} diff --git a/Emby.Server.Implementations/IO/ThrottledStream.cs b/Emby.Server.Implementations/IO/ThrottledStream.cs new file mode 100644 index 000000000..81760b639 --- /dev/null +++ b/Emby.Server.Implementations/IO/ThrottledStream.cs @@ -0,0 +1,394 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace Emby.Server.Implementations.IO +{ + /// <summary> + /// Class for streaming data with throttling support. + /// </summary> + public class ThrottledStream : Stream + { + /// <summary> + /// A constant used to specify an infinite number of bytes that can be transferred per second. + /// </summary> + public const long Infinite = 0; + + #region Private members + /// <summary> + /// The base stream. + /// </summary> + private readonly Stream _baseStream; + + /// <summary> + /// The maximum bytes per second that can be transferred through the base stream. + /// </summary> + private long _maximumBytesPerSecond; + + /// <summary> + /// The number of bytes that has been transferred since the last throttle. + /// </summary> + private long _byteCount; + + /// <summary> + /// The start time in milliseconds of the last throttle. + /// </summary> + private long _start; + #endregion + + #region Properties + /// <summary> + /// Gets the current milliseconds. + /// </summary> + /// <value>The current milliseconds.</value> + protected long CurrentMilliseconds + { + get + { + return Environment.TickCount; + } + } + + /// <summary> + /// Gets or sets the maximum bytes per second that can be transferred through the base stream. + /// </summary> + /// <value>The maximum bytes per second.</value> + public long MaximumBytesPerSecond + { + get + { + return _maximumBytesPerSecond; + } + set + { + if (MaximumBytesPerSecond != value) + { + _maximumBytesPerSecond = value; + Reset(); + } + } + } + + /// <summary> + /// Gets a value indicating whether the current stream supports reading. + /// </summary> + /// <returns>true if the stream supports reading; otherwise, false.</returns> + public override bool CanRead + { + get + { + return _baseStream.CanRead; + } + } + + /// <summary> + /// Gets a value indicating whether the current stream supports seeking. + /// </summary> + /// <value></value> + /// <returns>true if the stream supports seeking; otherwise, false.</returns> + public override bool CanSeek + { + get + { + return _baseStream.CanSeek; + } + } + + /// <summary> + /// Gets a value indicating whether the current stream supports writing. + /// </summary> + /// <value></value> + /// <returns>true if the stream supports writing; otherwise, false.</returns> + public override bool CanWrite + { + get + { + return _baseStream.CanWrite; + } + } + + /// <summary> + /// Gets the length in bytes of the stream. + /// </summary> + /// <value></value> + /// <returns>A long value representing the length of the stream in bytes.</returns> + /// <exception cref="T:System.NotSupportedException">The base stream does not support seeking. </exception> + /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> + public override long Length + { + get + { + return _baseStream.Length; + } + } + + /// <summary> + /// Gets or sets the position within the current stream. + /// </summary> + /// <value></value> + /// <returns>The current position within the stream.</returns> + /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> + /// <exception cref="T:System.NotSupportedException">The base stream does not support seeking. </exception> + /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> + public override long Position + { + get + { + return _baseStream.Position; + } + set + { + _baseStream.Position = value; + } + } + #endregion + + public long MinThrottlePosition; + + #region Ctor + /// <summary> + /// Initializes a new instance of the <see cref="T:ThrottledStream"/> class. + /// </summary> + /// <param name="baseStream">The base stream.</param> + /// <param name="maximumBytesPerSecond">The maximum bytes per second that can be transferred through the base stream.</param> + /// <exception cref="ArgumentNullException">Thrown when <see cref="baseStream"/> is a null reference.</exception> + /// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="maximumBytesPerSecond"/> is a negative value.</exception> + public ThrottledStream(Stream baseStream, long maximumBytesPerSecond) + { + if (baseStream == null) + { + throw new ArgumentNullException("baseStream"); + } + + if (maximumBytesPerSecond < 0) + { + throw new ArgumentOutOfRangeException("maximumBytesPerSecond", + maximumBytesPerSecond, "The maximum number of bytes per second can't be negative."); + } + + _baseStream = baseStream; + _maximumBytesPerSecond = maximumBytesPerSecond; + _start = CurrentMilliseconds; + _byteCount = 0; + } + #endregion + + #region Public methods + /// <summary> + /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. + /// </summary> + /// <exception cref="T:System.IO.IOException">An I/O error occurs.</exception> + public override void Flush() + { + _baseStream.Flush(); + } + + /// <summary> + /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. + /// </summary> + /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param> + /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param> + /// <param name="count">The maximum number of bytes to be read from the current stream.</param> + /// <returns> + /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. + /// </returns> + /// <exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception> + /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> + /// <exception cref="T:System.NotSupportedException">The base stream does not support reading. </exception> + /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception> + /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> + /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception> + public override int Read(byte[] buffer, int offset, int count) + { + Throttle(count); + + return _baseStream.Read(buffer, offset, count); + } + + /// <summary> + /// Sets the position within the current stream. + /// </summary> + /// <param name="offset">A byte offset relative to the origin parameter.</param> + /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position.</param> + /// <returns> + /// The new position within the current stream. + /// </returns> + /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> + /// <exception cref="T:System.NotSupportedException">The base stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception> + /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> + public override long Seek(long offset, SeekOrigin origin) + { + return _baseStream.Seek(offset, origin); + } + + /// <summary> + /// Sets the length of the current stream. + /// </summary> + /// <param name="value">The desired length of the current stream in bytes.</param> + /// <exception cref="T:System.NotSupportedException">The base stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception> + /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> + /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> + public override void SetLength(long value) + { + _baseStream.SetLength(value); + } + + private long _bytesWritten; + + /// <summary> + /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. + /// </summary> + /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param> + /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param> + /// <param name="count">The number of bytes to be written to the current stream.</param> + /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> + /// <exception cref="T:System.NotSupportedException">The base stream does not support writing. </exception> + /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> + /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception> + /// <exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception> + /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception> + public override void Write(byte[] buffer, int offset, int count) + { + Throttle(count); + + _baseStream.Write(buffer, offset, count); + + _bytesWritten += count; + } + + public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + await ThrottleAsync(count, cancellationToken).ConfigureAwait(false); + + await _baseStream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); + + _bytesWritten += count; + } + + /// <summary> + /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>. + /// </summary> + /// <returns> + /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>. + /// </returns> + public override string ToString() + { + return _baseStream.ToString(); + } + #endregion + + private bool ThrottleCheck(int bufferSizeInBytes) + { + if (_bytesWritten < MinThrottlePosition) + { + return false; + } + + // Make sure the buffer isn't empty. + if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0) + { + return false; + } + + return true; + } + + #region Protected methods + /// <summary> + /// Throttles for the specified buffer size in bytes. + /// </summary> + /// <param name="bufferSizeInBytes">The buffer size in bytes.</param> + protected void Throttle(int bufferSizeInBytes) + { + if (!ThrottleCheck(bufferSizeInBytes)) + { + return ; + } + + _byteCount += bufferSizeInBytes; + long elapsedMilliseconds = CurrentMilliseconds - _start; + + if (elapsedMilliseconds > 0) + { + // Calculate the current bps. + long bps = _byteCount * 1000L / elapsedMilliseconds; + + // If the bps are more then the maximum bps, try to throttle. + if (bps > _maximumBytesPerSecond) + { + // Calculate the time to sleep. + long wakeElapsed = _byteCount * 1000L / _maximumBytesPerSecond; + int toSleep = (int)(wakeElapsed - elapsedMilliseconds); + + if (toSleep > 1) + { + try + { + // The time to sleep is more then a millisecond, so sleep. + var task = Task.Delay(toSleep); + Task.WaitAll(task); + } + catch + { + // Eatup ThreadAbortException. + } + + // A sleep has been done, reset. + Reset(); + } + } + } + } + + protected async Task ThrottleAsync(int bufferSizeInBytes, CancellationToken cancellationToken) + { + if (!ThrottleCheck(bufferSizeInBytes)) + { + return; + } + + _byteCount += bufferSizeInBytes; + long elapsedMilliseconds = CurrentMilliseconds - _start; + + if (elapsedMilliseconds > 0) + { + // Calculate the current bps. + long bps = _byteCount * 1000L / elapsedMilliseconds; + + // If the bps are more then the maximum bps, try to throttle. + if (bps > _maximumBytesPerSecond) + { + // Calculate the time to sleep. + long wakeElapsed = _byteCount * 1000L / _maximumBytesPerSecond; + int toSleep = (int)(wakeElapsed - elapsedMilliseconds); + + if (toSleep > 1) + { + // The time to sleep is more then a millisecond, so sleep. + await Task.Delay(toSleep, cancellationToken).ConfigureAwait(false); + + // A sleep has been done, reset. + Reset(); + } + } + } + } + + /// <summary> + /// Will reset the bytecount to 0 and reset the start time to the current time. + /// </summary> + protected void Reset() + { + long difference = CurrentMilliseconds - _start; + + // Only reset counters when a known history is available of more then 1 second. + if (difference > 1000) + { + _byteCount = 0; + _start = CurrentMilliseconds; + } + } + #endregion + } +}
\ No newline at end of file |
