From 8885bbb60f1f2e81e5fb0cf9d6abd1b1f5bddd61 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 8 Mar 2014 21:28:40 -0800 Subject: [PATCH] OS X and linux can be treated separately --- src/NzbDrone.Api/System/SystemModule.cs | 12 ++- .../EnsureThat/EnsureStringExtensions.cs | 2 +- .../EnvironmentInfo/AppFolderFactory.cs | 2 +- .../EnvironmentInfo/AppFolderInfo.cs | 2 +- src/NzbDrone.Common/EnvironmentInfo/OsInfo.cs | 77 +++++++++++++++---- .../EnvironmentInfo/RuntimeInfo.cs | 2 +- .../Instrumentation/LogTargets.cs | 2 +- src/NzbDrone.Common/PathEqualityComparer.cs | 2 +- src/NzbDrone.Common/PathExtensions.cs | 8 +- .../Lifecycle/LifecycleService.cs | 2 +- .../NotUnpackingSpecification.cs | 2 +- .../MediaFiles/RecycleBinProvider.cs | 2 +- .../Update/UpdateCheckService.cs | 2 +- .../AccessControl/FirewallAdapter.cs | 2 +- src/NzbDrone.Host/ApplicationServer.cs | 4 +- src/NzbDrone.Host/Bootstrap.cs | 4 +- src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs | 2 +- src/NzbDrone.Test.Common/StringExtensions.cs | 2 +- src/NzbDrone.Test.Common/TestBase.cs | 4 +- src/UI/.idea/jsLinters/jshint.xml | 4 +- src/UI/Handlebars/Helpers/System.js | 4 +- .../Permissions/PermissionsViewTemplate.html | 4 +- 22 files changed, 99 insertions(+), 48 deletions(-) diff --git a/src/NzbDrone.Api/System/SystemModule.cs b/src/NzbDrone.Api/System/SystemModule.cs index 5acd4d881..b56f1d14b 100644 --- a/src/NzbDrone.Api/System/SystemModule.cs +++ b/src/NzbDrone.Api/System/SystemModule.cs @@ -16,7 +16,11 @@ namespace NzbDrone.Api.System private readonly IConfigFileProvider _configFileProvider; private readonly IDatabase _database; - public SystemModule(IAppFolderInfo appFolderInfo, IRuntimeInfo runtimeInfo, IRouteCacheProvider routeCacheProvider, IConfigFileProvider configFileProvider, IDatabase database) + public SystemModule(IAppFolderInfo appFolderInfo, + IRuntimeInfo runtimeInfo, + IRouteCacheProvider routeCacheProvider, + IConfigFileProvider configFileProvider, + IDatabase database) : base("system") { _appFolderInfo = appFolderInfo; @@ -41,8 +45,10 @@ namespace NzbDrone.Api.System StartupPath = _appFolderInfo.StartUpFolder, AppData = _appFolderInfo.GetAppDataPath(), OsVersion = OsInfo.Version.ToString(), + IsMonoRuntime = OsInfo.IsMono, IsMono = OsInfo.IsMono, - IsLinux = OsInfo.IsLinux, + IsLinux = OsInfo.IsMono, + IsOsx = OsInfo.IsOsx, IsWindows = OsInfo.IsWindows, Branch = _configFileProvider.Branch, Authentication = _configFileProvider.AuthenticationEnabled, @@ -50,10 +56,8 @@ namespace NzbDrone.Api.System SqliteVersion = _database.Version, UrlBase = _configFileProvider.UrlBase }.AsResponse(); - } - private Response GetRoutes() { return _routeCacheProvider.GetCache().Values.AsResponse(); diff --git a/src/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs b/src/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs index ede83ca40..d8bade8cf 100644 --- a/src/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs +++ b/src/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs @@ -100,7 +100,7 @@ namespace NzbDrone.Common.EnsureThat if (param.Value.IsPathValid()) return param; - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { throw ExceptionFactory.CreateForParamValidation(param.Name, string.Format("value [{0}] is not a valid *nix path. paths must start with /", param.Value)); } diff --git a/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs b/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs index c49d4864b..d45f4269f 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs @@ -32,7 +32,7 @@ namespace NzbDrone.Common.EnvironmentInfo { _diskProvider.EnsureFolder(_appFolderInfo.AppDataFolder); - if (!OsInfo.IsLinux) + if (!OsInfo.IsMono) { SetPermissions(); } diff --git a/src/NzbDrone.Common/EnvironmentInfo/AppFolderInfo.cs b/src/NzbDrone.Common/EnvironmentInfo/AppFolderInfo.cs index 3a5bcd019..da1e7cfaa 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/AppFolderInfo.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/AppFolderInfo.cs @@ -17,7 +17,7 @@ namespace NzbDrone.Common.EnvironmentInfo public AppFolderInfo(IStartupContext startupContext) { - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { DATA_SPECIAL_FOLDER = Environment.SpecialFolder.ApplicationData; } diff --git a/src/NzbDrone.Common/EnvironmentInfo/OsInfo.cs b/src/NzbDrone.Common/EnvironmentInfo/OsInfo.cs index 3b769bfea..16894f00c 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/OsInfo.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/OsInfo.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; namespace NzbDrone.Common.EnvironmentInfo { @@ -7,34 +8,80 @@ namespace NzbDrone.Common.EnvironmentInfo static OsInfo() { + var platform = (int)Environment.OSVersion.Platform; + Version = Environment.OSVersion.Version; - IsMono = Type.GetType("Mono.Runtime") != null; - int platform = (int)Environment.OSVersion.Platform; - IsLinux = (platform == 4) || (platform == 6) || (platform == 128); - + IsMonoRuntime = Type.GetType("Mono.Runtime") != null; + IsMono = (platform == 4) || (platform == 6) || (platform == 128); + IsOsx = IsRunningOnMac(); + IsLinux = IsMono && !IsOsx; + IsWindows = !IsMono; + + FirstDayOfWeek = DateTime.Today.GetFirstDayOfWeek().DayOfWeek; + + if (!IsMono) + { + Os = Os.Windows; + } + + else + { + Os = IsOsx ? Os.Osx : Os.Linux; + } } public static Version Version { get; private set; } - + public static bool IsMonoRuntime { get; private set; } public static bool IsMono { get; private set; } - public static bool IsLinux { get; private set; } + public static bool IsOsx { get; private set; } + public static bool IsWindows { get; private set; } + public static Os Os { get; private set; } + public static DayOfWeek FirstDayOfWeek { get; private set; } + + //Borrowed from: https://github.com/jpobst/Pinta/blob/master/Pinta.Core/Managers/SystemManager.cs + //From Managed.Windows.Forms/XplatUI + [DllImport("libc")] + static extern int uname(IntPtr buf); - public static bool IsWindows + static bool IsRunningOnMac() { - get + var buf = IntPtr.Zero; + + try { - return !IsLinux; - } - } + buf = Marshal.AllocHGlobal(8192); + // This is a hacktastic way of getting sysname from uname () + if (uname(buf) == 0) + { + var os = Marshal.PtrToStringAnsi(buf); - public static DayOfWeek FirstDayOfWeek - { - get + if (os == "Darwin") + { + return true; + } + } + } + catch { - return DateTime.Today.GetFirstDayOfWeek().DayOfWeek; } + finally + { + if (buf != IntPtr.Zero) + { + Marshal.FreeHGlobal(buf); + } + } + + return false; } } + + public enum Os + { + Windows, + Linux, + Osx + } } \ No newline at end of file diff --git a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs index 82369c35b..8cef57af6 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs @@ -78,7 +78,7 @@ namespace NzbDrone.Common.EnvironmentInfo return (OsInfo.IsWindows && IsUserInteractive && ProcessName.Equals(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME, StringComparison.InvariantCultureIgnoreCase)) || - OsInfo.IsLinux; + OsInfo.IsMono; } } diff --git a/src/NzbDrone.Common/Instrumentation/LogTargets.cs b/src/NzbDrone.Common/Instrumentation/LogTargets.cs index f44ce456c..67e8f69d3 100644 --- a/src/NzbDrone.Common/Instrumentation/LogTargets.cs +++ b/src/NzbDrone.Common/Instrumentation/LogTargets.cs @@ -31,7 +31,7 @@ namespace NzbDrone.Common.Instrumentation } else { - if (inConsole && (OsInfo.IsLinux || new RuntimeInfo(null, new ServiceProvider(new ProcessProvider())).IsUserInteractive)) + if (inConsole && (OsInfo.IsMono || new RuntimeInfo(null, new ServiceProvider(new ProcessProvider())).IsUserInteractive)) { RegisterConsole(); } diff --git a/src/NzbDrone.Common/PathEqualityComparer.cs b/src/NzbDrone.Common/PathEqualityComparer.cs index ce55c50e8..88df4b480 100644 --- a/src/NzbDrone.Common/PathEqualityComparer.cs +++ b/src/NzbDrone.Common/PathEqualityComparer.cs @@ -20,7 +20,7 @@ namespace NzbDrone.Common public int GetHashCode(string obj) { - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { return obj.CleanFilePath().GetHashCode(); } diff --git a/src/NzbDrone.Common/PathExtensions.cs b/src/NzbDrone.Common/PathExtensions.cs index c08770fc8..16a9bddd1 100644 --- a/src/NzbDrone.Common/PathExtensions.cs +++ b/src/NzbDrone.Common/PathExtensions.cs @@ -29,7 +29,7 @@ namespace NzbDrone.Common var info = new FileInfo(path.Trim()); - if (!OsInfo.IsLinux && info.FullName.StartsWith(@"\\")) //UNC + if (!OsInfo.IsMono && info.FullName.StartsWith(@"\\")) //UNC { return info.FullName.TrimEnd('/', '\\', ' '); } @@ -39,7 +39,7 @@ namespace NzbDrone.Common public static bool PathEquals(this string firstPath, string secondPath) { - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { if (firstPath.Equals(secondPath)) return true; return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath()); @@ -58,7 +58,7 @@ namespace NzbDrone.Common return false; } - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { return path.StartsWith(Path.DirectorySeparatorChar.ToString()); } @@ -97,7 +97,7 @@ namespace NzbDrone.Common public static string GetActualCasing(this string path) { - if (OsInfo.IsLinux || path.StartsWith("\\")) + if (OsInfo.IsMono || path.StartsWith("\\")) { return path; } diff --git a/src/NzbDrone.Core/Lifecycle/LifecycleService.cs b/src/NzbDrone.Core/Lifecycle/LifecycleService.cs index 25199b49f..8b2f3a21e 100644 --- a/src/NzbDrone.Core/Lifecycle/LifecycleService.cs +++ b/src/NzbDrone.Core/Lifecycle/LifecycleService.cs @@ -48,7 +48,7 @@ namespace NzbDrone.Core.Lifecycle { _logger.Info("Restart requested."); - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { _processProvider.SpawnNewProcess(_runtimeInfo.ExecutingApplication, "--terminateexisting --nobrowser"); } diff --git a/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/NotUnpackingSpecification.cs b/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/NotUnpackingSpecification.cs index 50279a4e2..c5ebd44ec 100644 --- a/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/NotUnpackingSpecification.cs +++ b/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/NotUnpackingSpecification.cs @@ -36,7 +36,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications { if (Directory.GetParent(localEpisode.Path).Name.StartsWith(workingFolder)) { - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { _logger.Trace("{0} is still being unpacked", localEpisode.Path); return false; diff --git a/src/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs b/src/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs index 4c5140c9a..0dc62b613 100644 --- a/src/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs +++ b/src/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs @@ -73,7 +73,7 @@ namespace NzbDrone.Core.MediaFiles { logger.Info("Recycling Bin has not been configured, deleting permanently."); - if (!OsInfo.IsLinux) + if (!OsInfo.IsMono) { logger.Trace(_diskProvider.GetFileAttributes(path)); } diff --git a/src/NzbDrone.Core/Update/UpdateCheckService.cs b/src/NzbDrone.Core/Update/UpdateCheckService.cs index 32cb64be2..e8d853305 100644 --- a/src/NzbDrone.Core/Update/UpdateCheckService.cs +++ b/src/NzbDrone.Core/Update/UpdateCheckService.cs @@ -27,7 +27,7 @@ namespace NzbDrone.Core.Update public UpdatePackage AvailableUpdate() { - if (OsInfo.IsLinux) return null; + if (OsInfo.IsMono) return null; var latestAvailable = _updatePackageProvider.GetLatestUpdate(_configFileProvider.Branch, BuildInfo.Version); diff --git a/src/NzbDrone.Host/AccessControl/FirewallAdapter.cs b/src/NzbDrone.Host/AccessControl/FirewallAdapter.cs index c252e3e9e..556f47baf 100644 --- a/src/NzbDrone.Host/AccessControl/FirewallAdapter.cs +++ b/src/NzbDrone.Host/AccessControl/FirewallAdapter.cs @@ -90,7 +90,7 @@ namespace NzbDrone.Host.AccessControl private bool IsFirewallEnabled() { - if (OsInfo.IsLinux) return false; + if (OsInfo.IsMono) return false; try { diff --git a/src/NzbDrone.Host/ApplicationServer.cs b/src/NzbDrone.Host/ApplicationServer.cs index 946334ec6..1e6a5f713 100644 --- a/src/NzbDrone.Host/ApplicationServer.cs +++ b/src/NzbDrone.Host/ApplicationServer.cs @@ -53,7 +53,7 @@ namespace NzbDrone.Host public void Start() { - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { Console.CancelKeyPress += (sender, eventArgs) => _processProvider.Kill(_processProvider.GetCurrentProcess().Id); } @@ -90,7 +90,7 @@ namespace NzbDrone.Host public void Handle(ApplicationShutdownRequested message) { - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { _processProvider.Kill(_processProvider.GetCurrentProcess().Id); } diff --git a/src/NzbDrone.Host/Bootstrap.cs b/src/NzbDrone.Host/Bootstrap.cs index 1b0d9fa70..7a4ef8ff5 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -106,12 +106,12 @@ namespace NzbDrone.Host return ApplicationModes.Help; } - if (!OsInfo.IsLinux && startupContext.InstallService) + if (!OsInfo.IsMono && startupContext.InstallService) { return ApplicationModes.InstallService; } - if (!OsInfo.IsLinux && startupContext.UninstallService) + if (!OsInfo.IsMono && startupContext.UninstallService) { return ApplicationModes.UninstallService; } diff --git a/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs b/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs index 2c7e4b4f7..437a9bde4 100644 --- a/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs +++ b/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs @@ -173,7 +173,7 @@ namespace NzbDrone.Test.Common.AutoMoq { var assemblyName = "NzbDrone.Windows"; - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { assemblyName = "NzbDrone.Mono"; } diff --git a/src/NzbDrone.Test.Common/StringExtensions.cs b/src/NzbDrone.Test.Common/StringExtensions.cs index 7ec3f4698..09b9b5b2d 100644 --- a/src/NzbDrone.Test.Common/StringExtensions.cs +++ b/src/NzbDrone.Test.Common/StringExtensions.cs @@ -7,7 +7,7 @@ namespace NzbDrone.Test.Common { public static string AsOsAgnostic(this string path) { - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { if (path.Length > 2 && path[1] == ':') { diff --git a/src/NzbDrone.Test.Common/TestBase.cs b/src/NzbDrone.Test.Common/TestBase.cs index 470cba8e1..c42fca8a9 100644 --- a/src/NzbDrone.Test.Common/TestBase.cs +++ b/src/NzbDrone.Test.Common/TestBase.cs @@ -124,7 +124,7 @@ namespace NzbDrone.Test.Common protected void WindowsOnly() { - if (OsInfo.IsLinux) + if (OsInfo.IsMono) { throw new IgnoreException("windows specific test"); } @@ -133,7 +133,7 @@ namespace NzbDrone.Test.Common protected void LinuxOnly() { - if (!OsInfo.IsLinux) + if (!OsInfo.IsMono) { throw new IgnoreException("linux specific test"); } diff --git a/src/UI/.idea/jsLinters/jshint.xml b/src/UI/.idea/jsLinters/jshint.xml index 4e0df49ad..e85398a55 100644 --- a/src/UI/.idea/jsLinters/jshint.xml +++ b/src/UI/.idea/jsLinters/jshint.xml @@ -8,16 +8,16 @@