diff options
| author | Andrew Rabert <6550543+nvllsvm@users.noreply.github.com> | 2019-01-22 18:13:47 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-22 18:13:47 -0500 |
| commit | 28483bdb54be96ae83e0fded097f534d7e26ba1e (patch) | |
| tree | e7f4b92326417ebf55eecdf68a01d2c3b9e660d7 /Emby.IsoMounting/IsoMounter/LinuxIsoManager.cs | |
| parent | 920c39454c05e979eabe81877269cd4517a03ccf (diff) | |
| parent | 8106c8393b711a7e1d40487e3caf2b014decbe28 (diff) | |
Merge pull request #651 from jellyfin/release-10.1.0
Release 10.1.0
Diffstat (limited to 'Emby.IsoMounting/IsoMounter/LinuxIsoManager.cs')
| -rw-r--r-- | Emby.IsoMounting/IsoMounter/LinuxIsoManager.cs | 154 |
1 files changed, 87 insertions, 67 deletions
diff --git a/Emby.IsoMounting/IsoMounter/LinuxIsoManager.cs b/Emby.IsoMounting/IsoMounter/LinuxIsoManager.cs index faaed482a..ac486f167 100644 --- a/Emby.IsoMounting/IsoMounter/LinuxIsoManager.cs +++ b/Emby.IsoMounting/IsoMounter/LinuxIsoManager.cs @@ -1,19 +1,19 @@ using System; using System.IO; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Model.Diagnostics; using MediaBrowser.Model.IO; -using Microsoft.Extensions.Logging; using MediaBrowser.Model.System; -using System.Runtime.InteropServices; +using Microsoft.Extensions.Logging; namespace IsoMounter { public class LinuxIsoManager : IIsoMounter { [DllImport("libc", SetLastError = true)] - public static extern uint getuid(); + static extern uint getuid(); #region Private Fields @@ -44,13 +44,13 @@ namespace IsoMounter _logger.LogDebug( "[{0}] System PATH is currently set to [{1}].", Name, - EnvironmentInfo.GetEnvironmentVariable("PATH") ?? "" + Environment.GetEnvironmentVariable("PATH") ?? "" ); _logger.LogDebug( "[{0}] System path separator is [{1}].", Name, - EnvironmentInfo.PathSeparator + Path.PathSeparator ); _logger.LogDebug( @@ -87,9 +87,12 @@ namespace IsoMounter UmountCommand ); - if (!String.IsNullOrEmpty(SudoCommand) && !String.IsNullOrEmpty(MountCommand) && !String.IsNullOrEmpty(UmountCommand)) { + if (!string.IsNullOrEmpty(SudoCommand) && !string.IsNullOrEmpty(MountCommand) && !string.IsNullOrEmpty(UmountCommand)) + { ExecutablesAvailable = true; - } else { + } + else + { ExecutablesAvailable = false; } @@ -99,44 +102,36 @@ namespace IsoMounter #region Interface Implementation for IIsoMounter - public bool IsInstalled { - get { - return true; - } - } + public bool IsInstalled => true; - public string Name { - get { return "LinuxMount"; } - } + public string Name => "LinuxMount"; - public bool RequiresInstallation { - get { - return false; - } - } + public bool RequiresInstallation => false; public bool CanMount(string path) { - if (EnvironmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Linux) { - _logger.LogInformation( - "[{0}] Checking we can attempt to mount [{1}], Extension = [{2}], Operating System = [{3}], Executables Available = [{4}].", - Name, - path, - Path.GetExtension(path), - EnvironmentInfo.OperatingSystem, - ExecutablesAvailable.ToString() - ); - - if (ExecutablesAvailable) { - return string.Equals(Path.GetExtension(path), ".iso", StringComparison.OrdinalIgnoreCase); - } else { - return false; - } - } else { + if (EnvironmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Linux) + { return false; } + _logger.LogInformation( + "[{0}] Checking we can attempt to mount [{1}], Extension = [{2}], Operating System = [{3}], Executables Available = [{4}].", + Name, + path, + Path.GetExtension(path), + EnvironmentInfo.OperatingSystem, + ExecutablesAvailable.ToString() + ); + if (ExecutablesAvailable) + { + return string.Equals(Path.GetExtension(path), ".iso", StringComparison.OrdinalIgnoreCase); + } + else + { + return false; + } } public Task Install(CancellationToken cancellationToken) @@ -146,11 +141,13 @@ namespace IsoMounter public Task<IIsoMount> Mount(string isoPath, CancellationToken cancellationToken) { - if (MountISO(isoPath, out LinuxMount mountedISO)) { + if (MountISO(isoPath, out LinuxMount mountedISO)) + { return Task.FromResult<IIsoMount>(mountedISO); } - else { - throw new IOException(String.Format( + else + { + throw new IOException(string.Format( "An error occurred trying to mount image [$0].", isoPath )); @@ -178,7 +175,8 @@ namespace IsoMounter protected virtual void Dispose(bool disposing) { - if (disposed) { + if (disposed) + { return; } @@ -188,7 +186,8 @@ namespace IsoMounter disposing.ToString() ); - if (disposing) { + if (disposing) + { // // Free managed objects here. @@ -211,18 +210,17 @@ namespace IsoMounter private string GetFullPathForExecutable(string name) { - foreach (string test in (EnvironmentInfo.GetEnvironmentVariable("PATH") ?? "").Split(EnvironmentInfo.PathSeparator)) { - + foreach (string test in (Environment.GetEnvironmentVariable("PATH") ?? "").Split(Path.PathSeparator)) + { string path = test.Trim(); - if (!String.IsNullOrEmpty(path) && FileSystem.FileExists(path = Path.Combine(path, name))) { + if (!string.IsNullOrEmpty(path) && FileSystem.FileExists(path = Path.Combine(path, name))) + { return FileSystem.GetFullPath(path); } - } - return String.Empty; - + return string.Empty; } private uint GetUID() @@ -247,7 +245,8 @@ namespace IsoMounter bool processFailed = false; var process = ProcessFactory.Create( - new ProcessOptions { + new ProcessOptions + { CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, @@ -285,9 +284,12 @@ namespace IsoMounter _logger.LogDebug(ex, "[{Name}] Unhandled exception executing command.", Name); } - if (!processFailed && process.ExitCode == 0) { + if (!processFailed && process.ExitCode == 0) + { return true; - } else { + } + else + { return false; } @@ -300,7 +302,8 @@ namespace IsoMounter string cmdFilename; string mountPoint = Path.Combine(MountPointRoot, Guid.NewGuid().ToString()); - if (!string.IsNullOrEmpty(isoPath)) { + if (!string.IsNullOrEmpty(isoPath)) + { _logger.LogInformation( "[{Name}] Attempting to mount [{Path}].", @@ -314,10 +317,12 @@ namespace IsoMounter mountPoint ); - } else { - + } + else + { + throw new ArgumentNullException(nameof(isoPath)); - + } try @@ -333,10 +338,13 @@ namespace IsoMounter throw new IOException("Unable to create mount point for " + isoPath); } - if (GetUID() == 0) { + if (GetUID() == 0) + { cmdFilename = MountCommand; cmdArguments = string.Format("\"{0}\" \"{1}\"", isoPath, mountPoint); - } else { + } + else + { cmdFilename = SudoCommand; cmdArguments = string.Format("\"{0}\" \"{1}\" \"{2}\"", MountCommand, isoPath, mountPoint); } @@ -348,7 +356,8 @@ namespace IsoMounter cmdArguments ); - if (ExecuteCommand(cmdFilename, cmdArguments)) { + if (ExecuteCommand(cmdFilename, cmdArguments)) + { _logger.LogInformation( "[{0}] ISO mount completed successfully.", @@ -357,7 +366,9 @@ namespace IsoMounter mountedISO = new LinuxMount(this, isoPath, mountPoint); - } else { + } + else + { _logger.LogInformation( "[{0}] ISO mount completed with errors.", @@ -387,7 +398,8 @@ namespace IsoMounter string cmdArguments; string cmdFilename; - if (mount != null) { + if (mount != null) + { _logger.LogInformation( "[{0}] Attempting to unmount ISO [{1}] mounted on [{2}].", @@ -396,16 +408,21 @@ namespace IsoMounter mount.MountedPath ); - } else { - + } + else + { + throw new ArgumentNullException(nameof(mount)); - + } - if (GetUID() == 0) { + if (GetUID() == 0) + { cmdFilename = UmountCommand; cmdArguments = string.Format("\"{0}\"", mount.MountedPath); - } else { + } + else + { cmdFilename = SudoCommand; cmdArguments = string.Format("\"{0}\" \"{1}\"", UmountCommand, mount.MountedPath); } @@ -417,14 +434,17 @@ namespace IsoMounter cmdArguments ); - if (ExecuteCommand(cmdFilename, cmdArguments)) { + if (ExecuteCommand(cmdFilename, cmdArguments)) + { _logger.LogInformation( "[{0}] ISO unmount completed successfully.", Name ); - } else { + } + else + { _logger.LogInformation( "[{0}] ISO unmount completed with errors.", @@ -444,7 +464,7 @@ namespace IsoMounter } #endregion - + #region Internal Methods internal void OnUnmount(LinuxMount mount) |
