diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index 866f1a0fd..ed5f73a3e 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -31,6 +31,7 @@ namespace NzbDrone.Common.Disk 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); @@ -324,42 +325,6 @@ namespace NzbDrone.Common.Disk 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 b5df2d460..3ed95fc4c 100644 --- a/src/NzbDrone.Common/Disk/IDiskProvider.cs +++ b/src/NzbDrone.Common/Disk/IDiskProvider.cs @@ -10,6 +10,7 @@ namespace NzbDrone.Common.Disk { 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 @@ namespace NzbDrone.Common.Disk 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 8fb4e0bf5..e6833aca0 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs @@ -66,7 +66,7 @@ namespace NzbDrone.Common.EnvironmentInfo { 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 9a832d5d1..cf52b4884 100644 --- a/src/NzbDrone.Mono/Disk/DiskProvider.cs +++ b/src/NzbDrone.Mono/Disk/DiskProvider.cs @@ -56,20 +56,12 @@ namespace NzbDrone.Mono.Disk public override void InheritFolderPermissions(string filename) { - Ensure.That(filename, () => filename).IsValidPath(); - try - { - var fs = File.GetAccessControl(filename); - fs.SetAccessRuleProtection(false, false); - File.SetAccessControl(filename, 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 f2bf301b4..8a7a45a52 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; @@ -39,9 +42,50 @@ namespace NzbDrone.Windows.Disk { Ensure.That(filename, () => filename).IsValidPath(); - var fs = File.GetAccessControl(filename); + var fileInfo = new FileInfo(filename); + var fs = fileInfo.GetAccessControl(AccessControlSections.Access); fs.SetAccessRuleProtection(false, false); - File.SetAccessControl(filename, 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)