From c1b6917afe73a8db3ce3fa6f2d5c088d4b84ce23 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sat, 5 Sep 2020 22:42:51 -0400 Subject: [PATCH] Moved Windows-only Permission function to Radarr.Windows Co-Authored-By: Taloth --- src/NzbDrone.Common/Disk/DiskProviderBase.cs | 40 +-------------- src/NzbDrone.Common/Disk/IDiskProvider.cs | 2 +- .../EnvironmentInfo/AppFolderFactory.cs | 2 +- src/NzbDrone.Mono/Disk/DiskProvider.cs | 17 ++----- src/NzbDrone.Windows/Disk/DiskProvider.cs | 50 +++++++++++++++++-- 5 files changed, 53 insertions(+), 58 deletions(-) diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index 979075104..af813e8a7 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Security.AccessControl; -using System.Security.Principal; using NLog; using NzbDrone.Common.EnsureThat; using NzbDrone.Common.EnvironmentInfo; @@ -31,6 +29,7 @@ public static StringComparison PathStringComparison public abstract long? GetAvailableSpace(string path); public abstract void InheritFolderPermissions(string filename); + public abstract void SetEveryonePermissions(string filename); public abstract void SetPermissions(string path, string mask); public abstract void CopyPermissions(string sourcePath, string targetPath); public abstract long? GetTotalSize(string path); @@ -346,43 +345,6 @@ public string GetParentFolder(string path) return parent.FullName; } - public void SetPermissions(string filename, WellKnownSidType accountSid, FileSystemRights rights, AccessControlType controlType) - { - try - { - var sid = new SecurityIdentifier(accountSid, null); - - var directoryInfo = new DirectoryInfo(filename); - var directorySecurity = directoryInfo.GetAccessControl(AccessControlSections.Access); - - var rules = directorySecurity.GetAccessRules(true, false, typeof(SecurityIdentifier)); - - if (rules.OfType().Any(acl => acl.AccessControlType == controlType && (acl.FileSystemRights & rights) == rights && acl.IdentityReference.Equals(sid))) - { - return; - } - - var accessRule = new FileSystemAccessRule(sid, - rights, - InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, - PropagationFlags.InheritOnly, - controlType); - - bool modified; - directorySecurity.ModifyAccessRule(AccessControlModification.Add, accessRule, out modified); - - if (modified) - { - directoryInfo.SetAccessControl(directorySecurity); - } - } - catch (Exception e) - { - Logger.Warn(e, "Couldn't set permission for {0}. account:{1} rights:{2} accessControlType:{3}", filename, accountSid, rights, controlType); - throw; - } - } - private static void RemoveReadOnly(string path) { if (File.Exists(path)) diff --git a/src/NzbDrone.Common/Disk/IDiskProvider.cs b/src/NzbDrone.Common/Disk/IDiskProvider.cs index 287fd96d4..629b300fc 100644 --- a/src/NzbDrone.Common/Disk/IDiskProvider.cs +++ b/src/NzbDrone.Common/Disk/IDiskProvider.cs @@ -10,6 +10,7 @@ public interface IDiskProvider { long? GetAvailableSpace(string path); void InheritFolderPermissions(string filename); + void SetEveryonePermissions(string filename); void SetPermissions(string path, string mask); void CopyPermissions(string sourcePath, string targetPath); long? GetTotalSize(string path); @@ -40,7 +41,6 @@ public interface IDiskProvider bool IsFileLocked(string path); string GetPathRoot(string path); string GetParentFolder(string path); - void SetPermissions(string filename, WellKnownSidType accountSid, FileSystemRights rights, AccessControlType controlType); FileAttributes GetFileAttributes(string path); void EmptyFolder(string path); string GetVolumeLabel(string path); diff --git a/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs b/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs index 6d5eb9135..d25c6b200 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs @@ -65,7 +65,7 @@ private void SetPermissions() { try { - _diskProvider.SetPermissions(_appFolderInfo.AppDataFolder, WellKnownSidType.WorldSid, FileSystemRights.Modify, AccessControlType.Allow); + _diskProvider.SetEveryonePermissions(_appFolderInfo.AppDataFolder); } catch (Exception ex) { diff --git a/src/NzbDrone.Mono/Disk/DiskProvider.cs b/src/NzbDrone.Mono/Disk/DiskProvider.cs index 76cd2f803..966313b5d 100644 --- a/src/NzbDrone.Mono/Disk/DiskProvider.cs +++ b/src/NzbDrone.Mono/Disk/DiskProvider.cs @@ -53,21 +53,10 @@ public override IMount GetMount(string path) public override void InheritFolderPermissions(string filename) { - Ensure.That(filename, () => filename).IsValidPath(); + } - try - { - var file = new FileInfo(filename); - var fs = file.GetAccessControl(); - fs.SetAccessRuleProtection(false, false); - file.SetAccessControl(fs); - } - catch (NotImplementedException) - { - } - catch (PlatformNotSupportedException) - { - } + public override void SetEveryonePermissions(string filename) + { } public override void SetPermissions(string path, string mask) diff --git a/src/NzbDrone.Windows/Disk/DiskProvider.cs b/src/NzbDrone.Windows/Disk/DiskProvider.cs index b2025a1f2..943058a6e 100644 --- a/src/NzbDrone.Windows/Disk/DiskProvider.cs +++ b/src/NzbDrone.Windows/Disk/DiskProvider.cs @@ -1,6 +1,9 @@ using System; using System.IO; +using System.Linq; using System.Runtime.InteropServices; +using System.Security.AccessControl; +using System.Security.Principal; using NLog; using NzbDrone.Common.Disk; using NzbDrone.Common.EnsureThat; @@ -41,10 +44,51 @@ public override void InheritFolderPermissions(string filename) { Ensure.That(filename, () => filename).IsValidPath(); - var file = new FileInfo(filename); - var fs = file.GetAccessControl(); + var fileInfo = new FileInfo(filename); + var fs = fileInfo.GetAccessControl(AccessControlSections.Access); fs.SetAccessRuleProtection(false, false); - file.SetAccessControl(fs); + fileInfo.SetAccessControl(fs); + } + + public override void SetEveryonePermissions(string filename) + { + var accountSid = WellKnownSidType.WorldSid; + var rights = FileSystemRights.Modify; + var controlType = AccessControlType.Allow; + + try + { + var sid = new SecurityIdentifier(accountSid, null); + + var directoryInfo = new DirectoryInfo(filename); + var directorySecurity = directoryInfo.GetAccessControl(AccessControlSections.Access); + + var rules = directorySecurity.GetAccessRules(true, false, typeof(SecurityIdentifier)); + + if (rules.OfType().Any(acl => acl.AccessControlType == controlType && (acl.FileSystemRights & rights) == rights && acl.IdentityReference.Equals(sid))) + { + return; + } + + var accessRule = new FileSystemAccessRule(sid, + rights, + InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, + PropagationFlags.InheritOnly, + controlType); + + bool modified; + directorySecurity.ModifyAccessRule(AccessControlModification.Add, accessRule, out modified); + + if (modified) + { + directoryInfo.SetAccessControl(directorySecurity); + } + } + catch (Exception e) + { + Logger.Warn(e, "Couldn't set permission for {0}. account:{1} rights:{2} accessControlType:{3}", filename, accountSid, rights, controlType); + throw; + } } public override void SetPermissions(string path, string mask)