aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/IO
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/IO')
-rw-r--r--Emby.Server.Implementations/IO/LibraryMonitor.cs15
-rw-r--r--Emby.Server.Implementations/IO/ManagedFileSystem.cs16
-rw-r--r--Emby.Server.Implementations/IO/StreamHelper.cs226
3 files changed, 146 insertions, 111 deletions
diff --git a/Emby.Server.Implementations/IO/LibraryMonitor.cs b/Emby.Server.Implementations/IO/LibraryMonitor.cs
index d47342511..df4dc41b9 100644
--- a/Emby.Server.Implementations/IO/LibraryMonitor.cs
+++ b/Emby.Server.Implementations/IO/LibraryMonitor.cs
@@ -10,8 +10,8 @@ using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.System;
-using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
+using OperatingSystem = MediaBrowser.Common.System.OperatingSystem;
namespace Emby.Server.Implementations.IO
{
@@ -127,7 +127,6 @@ namespace Emby.Server.Implementations.IO
private IServerConfigurationManager ConfigurationManager { get; set; }
private readonly IFileSystem _fileSystem;
- private readonly IEnvironmentInfo _environmentInfo;
/// <summary>
/// Initializes a new instance of the <see cref="LibraryMonitor" /> class.
@@ -136,14 +135,12 @@ namespace Emby.Server.Implementations.IO
ILoggerFactory loggerFactory,
ILibraryManager libraryManager,
IServerConfigurationManager configurationManager,
- IFileSystem fileSystem,
- IEnvironmentInfo environmentInfo)
+ IFileSystem fileSystem)
{
LibraryManager = libraryManager;
Logger = loggerFactory.CreateLogger(GetType().Name);
ConfigurationManager = configurationManager;
_fileSystem = fileSystem;
- _environmentInfo = environmentInfo;
}
private bool IsLibraryMonitorEnabled(BaseItem item)
@@ -267,7 +264,7 @@ namespace Emby.Server.Implementations.IO
return;
}
- if (_environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows)
+ if (OperatingSystem.Id != OperatingSystemId.Windows)
{
if (path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase) || path.StartsWith("smb://", StringComparison.OrdinalIgnoreCase))
{
@@ -276,12 +273,6 @@ namespace Emby.Server.Implementations.IO
}
}
- if (_environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Android)
- {
- // causing crashing
- return;
- }
-
// Already being watched
if (_fileSystemWatchers.ContainsKey(path))
{
diff --git a/Emby.Server.Implementations/IO/ManagedFileSystem.cs b/Emby.Server.Implementations/IO/ManagedFileSystem.cs
index 421592fad..47cea7269 100644
--- a/Emby.Server.Implementations/IO/ManagedFileSystem.cs
+++ b/Emby.Server.Implementations/IO/ManagedFileSystem.cs
@@ -8,6 +8,7 @@ using MediaBrowser.Common.Configuration;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.System;
using Microsoft.Extensions.Logging;
+using OperatingSystem = MediaBrowser.Common.System.OperatingSystem;
namespace Emby.Server.Implementations.IO
{
@@ -24,22 +25,19 @@ namespace Emby.Server.Implementations.IO
private readonly string _tempPath;
- private readonly IEnvironmentInfo _environmentInfo;
private readonly bool _isEnvironmentCaseInsensitive;
public ManagedFileSystem(
ILoggerFactory loggerFactory,
- IEnvironmentInfo environmentInfo,
IApplicationPaths applicationPaths)
{
Logger = loggerFactory.CreateLogger("FileSystem");
_supportsAsyncFileStreams = true;
_tempPath = applicationPaths.TempDirectory;
- _environmentInfo = environmentInfo;
- SetInvalidFileNameChars(environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows);
+ SetInvalidFileNameChars(OperatingSystem.Id == OperatingSystemId.Windows);
- _isEnvironmentCaseInsensitive = environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows;
+ _isEnvironmentCaseInsensitive = OperatingSystem.Id == OperatingSystemId.Windows;
}
public virtual void AddShortcutHandler(IShortcutHandler handler)
@@ -468,7 +466,7 @@ namespace Emby.Server.Implementations.IO
public virtual void SetHidden(string path, bool isHidden)
{
- if (_environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows)
+ if (OperatingSystem.Id != MediaBrowser.Model.System.OperatingSystemId.Windows)
{
return;
}
@@ -492,7 +490,7 @@ namespace Emby.Server.Implementations.IO
public virtual void SetReadOnly(string path, bool isReadOnly)
{
- if (_environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows)
+ if (OperatingSystem.Id != MediaBrowser.Model.System.OperatingSystemId.Windows)
{
return;
}
@@ -516,7 +514,7 @@ namespace Emby.Server.Implementations.IO
public virtual void SetAttributes(string path, bool isHidden, bool isReadOnly)
{
- if (_environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows)
+ if (OperatingSystem.Id != MediaBrowser.Model.System.OperatingSystemId.Windows)
{
return;
}
@@ -801,7 +799,7 @@ namespace Emby.Server.Implementations.IO
public virtual void SetExecutable(string path)
{
- if (_environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.OSX)
+ if (OperatingSystem.Id == MediaBrowser.Model.System.OperatingSystemId.Darwin)
{
RunProcess("chmod", "+x \"" + path + "\"", Path.GetDirectoryName(path));
}
diff --git a/Emby.Server.Implementations/IO/StreamHelper.cs b/Emby.Server.Implementations/IO/StreamHelper.cs
index 09cf4d4a3..d02cd84a0 100644
--- a/Emby.Server.Implementations/IO/StreamHelper.cs
+++ b/Emby.Server.Implementations/IO/StreamHelper.cs
@@ -1,4 +1,5 @@
using System;
+using System.Buffers;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
@@ -8,168 +9,213 @@ namespace Emby.Server.Implementations.IO
{
public class StreamHelper : IStreamHelper
{
+ private const int StreamCopyToBufferSize = 81920;
+
public async Task CopyToAsync(Stream source, Stream destination, int bufferSize, Action onStarted, CancellationToken cancellationToken)
{
- byte[] buffer = new byte[bufferSize];
- int read;
- while ((read = source.Read(buffer, 0, buffer.Length)) != 0)
+ byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
+ try
{
- cancellationToken.ThrowIfCancellationRequested();
+ int read;
+ while ((read = await source.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
- await destination.WriteAsync(buffer, 0, read).ConfigureAwait(false);
+ await destination.WriteAsync(buffer, 0, read).ConfigureAwait(false);
- if (onStarted != null)
- {
- onStarted();
- onStarted = null;
+ if (onStarted != null)
+ {
+ onStarted();
+ onStarted = null;
+ }
}
}
+ finally
+ {
+ ArrayPool<byte>.Shared.Return(buffer);
+ }
}
public async Task CopyToAsync(Stream source, Stream destination, int bufferSize, int emptyReadLimit, CancellationToken cancellationToken)
{
- byte[] buffer = new byte[bufferSize];
-
- if (emptyReadLimit <= 0)
+ byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
+ try
{
- int read;
- while ((read = source.Read(buffer, 0, buffer.Length)) != 0)
+ if (emptyReadLimit <= 0)
{
- cancellationToken.ThrowIfCancellationRequested();
+ int read;
+ while ((read = await source.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
- await destination.WriteAsync(buffer, 0, read).ConfigureAwait(false);
- }
+ await destination.WriteAsync(buffer, 0, read).ConfigureAwait(false);
+ }
- return;
- }
+ return;
+ }
- var eofCount = 0;
+ var eofCount = 0;
- while (eofCount < emptyReadLimit)
- {
- cancellationToken.ThrowIfCancellationRequested();
+ while (eofCount < emptyReadLimit)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
- var bytesRead = source.Read(buffer, 0, buffer.Length);
+ var bytesRead = await source.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
- if (bytesRead == 0)
- {
- eofCount++;
- await Task.Delay(50, cancellationToken).ConfigureAwait(false);
- }
- else
- {
- eofCount = 0;
+ if (bytesRead == 0)
+ {
+ eofCount++;
+ await Task.Delay(50, cancellationToken).ConfigureAwait(false);
+ }
+ else
+ {
+ eofCount = 0;
- await destination.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
+ await destination.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
+ }
}
}
+ finally
+ {
+ ArrayPool<byte>.Shared.Return(buffer);
+ }
}
- const int StreamCopyToBufferSize = 81920;
public async Task<int> CopyToAsync(Stream source, Stream destination, CancellationToken cancellationToken)
{
- var array = new byte[StreamCopyToBufferSize];
- int bytesRead;
- int totalBytesRead = 0;
-
- while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
+ byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamCopyToBufferSize);
+ try
{
- var bytesToWrite = bytesRead;
+ int totalBytesRead = 0;
- if (bytesToWrite > 0)
+ int bytesRead;
+ while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
- await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
+ var bytesToWrite = bytesRead;
- totalBytesRead += bytesRead;
+ if (bytesToWrite > 0)
+ {
+ await destination.WriteAsync(buffer, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
+
+ totalBytesRead += bytesRead;
+ }
}
- }
- return totalBytesRead;
+ return totalBytesRead;
+ }
+ finally
+ {
+ ArrayPool<byte>.Shared.Return(buffer);
+ }
}
public async Task<int> CopyToAsyncWithSyncRead(Stream source, Stream destination, CancellationToken cancellationToken)
{
- var array = new byte[StreamCopyToBufferSize];
- int bytesRead;
- int totalBytesRead = 0;
-
- while ((bytesRead = source.Read(array, 0, array.Length)) != 0)
+ byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamCopyToBufferSize);
+ try
{
- var bytesToWrite = bytesRead;
+ int bytesRead;
+ int totalBytesRead = 0;
- if (bytesToWrite > 0)
+ while ((bytesRead = source.Read(buffer, 0, buffer.Length)) != 0)
{
- await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
+ var bytesToWrite = bytesRead;
- totalBytesRead += bytesRead;
+ if (bytesToWrite > 0)
+ {
+ await destination.WriteAsync(buffer, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
+
+ totalBytesRead += bytesRead;
+ }
}
- }
- return totalBytesRead;
+ return totalBytesRead;
+ }
+ finally
+ {
+ ArrayPool<byte>.Shared.Return(buffer);
+ }
}
public async Task CopyToAsyncWithSyncRead(Stream source, Stream destination, long copyLength, CancellationToken cancellationToken)
{
- var array = new byte[StreamCopyToBufferSize];
- int bytesRead;
-
- while ((bytesRead = source.Read(array, 0, array.Length)) != 0)
+ byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamCopyToBufferSize);
+ try
{
- var bytesToWrite = Math.Min(bytesRead, copyLength);
+ int bytesRead;
- if (bytesToWrite > 0)
+ while ((bytesRead = source.Read(buffer, 0, buffer.Length)) != 0)
{
- await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
- }
+ var bytesToWrite = Math.Min(bytesRead, copyLength);
- copyLength -= bytesToWrite;
+ if (bytesToWrite > 0)
+ {
+ await destination.WriteAsync(buffer, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
+ }
- if (copyLength <= 0)
- {
- break;
+ copyLength -= bytesToWrite;
+
+ if (copyLength <= 0)
+ {
+ break;
+ }
}
}
+ finally
+ {
+ ArrayPool<byte>.Shared.Return(buffer);
+ }
}
public async Task CopyToAsync(Stream source, Stream destination, long copyLength, CancellationToken cancellationToken)
{
- var array = new byte[StreamCopyToBufferSize];
- int bytesRead;
-
- while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
+ byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamCopyToBufferSize);
+ try
{
- var bytesToWrite = Math.Min(bytesRead, copyLength);
+ int bytesRead;
- if (bytesToWrite > 0)
+ while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
- await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
- }
+ var bytesToWrite = Math.Min(bytesRead, copyLength);
- copyLength -= bytesToWrite;
+ if (bytesToWrite > 0)
+ {
+ await destination.WriteAsync(buffer, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
+ }
- if (copyLength <= 0)
- {
- break;
+ copyLength -= bytesToWrite;
+
+ if (copyLength <= 0)
+ {
+ break;
+ }
}
}
+ finally
+ {
+ ArrayPool<byte>.Shared.Return(buffer);
+ }
}
public async Task CopyUntilCancelled(Stream source, Stream target, int bufferSize, CancellationToken cancellationToken)
{
- byte[] buffer = new byte[bufferSize];
-
- while (!cancellationToken.IsCancellationRequested)
+ byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
+ try
{
- var bytesRead = await CopyToAsyncInternal(source, target, buffer, cancellationToken).ConfigureAwait(false);
-
- //var position = fs.Position;
- //_logger.LogDebug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
-
- if (bytesRead == 0)
+ while (!cancellationToken.IsCancellationRequested)
{
- await Task.Delay(100).ConfigureAwait(false);
+ var bytesRead = await CopyToAsyncInternal(source, target, buffer, cancellationToken).ConfigureAwait(false);
+
+ if (bytesRead == 0)
+ {
+ await Task.Delay(100).ConfigureAwait(false);
+ }
}
}
+ finally
+ {
+ ArrayPool<byte>.Shared.Return(buffer);
+ }
}
private static async Task<int> CopyToAsyncInternal(Stream source, Stream destination, byte[] buffer, CancellationToken cancellationToken)