From 3ea59cd91b2d249b0a3933da6fa5542d1552bf86 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sun, 20 Mar 2016 18:54:02 +0100 Subject: [PATCH] Don't set ACL if already set. --- src/NzbDrone.Common/Disk/DiskProviderBase.cs | 22 ++++++++++++++----- .../EnvironmentInfo/AppFolderFactory.cs | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index a241cf9d4..f494b7bbb 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -293,18 +293,30 @@ public void SetPermissions(string filename, WellKnownSidType accountSid, FileSys var sid = new SecurityIdentifier(accountSid, null); var directoryInfo = new DirectoryInfo(filename); - var directorySecurity = directoryInfo.GetAccessControl(); + 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.None, controlType); + PropagationFlags.InheritOnly, controlType); - directorySecurity.AddAccessRule(accessRule); - directoryInfo.SetAccessControl(directorySecurity); + bool modified; + directorySecurity.ModifyAccessRule(AccessControlModification.Add, accessRule, out modified); + + if (modified) + { + directoryInfo.SetAccessControl(directorySecurity); + } } catch (Exception e) { - Logger.Warn(e, string.Format("Couldn't set permission for {0}. account:{1} rights:{2} accessControlType:{3}", filename, accountSid, rights, controlType)); + Logger.Warn(e, "Couldn't set permission for {0}. account:{1} rights:{2} accessControlType:{3}", filename, accountSid, rights, controlType); throw; } diff --git a/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs b/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs index c1632f8bc..7132d539f 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs @@ -39,7 +39,7 @@ private void SetPermissions() { try { - _diskProvider.SetPermissions(_appFolderInfo.AppDataFolder, WellKnownSidType.WorldSid, FileSystemRights.FullControl, AccessControlType.Allow); + _diskProvider.SetPermissions(_appFolderInfo.AppDataFolder, WellKnownSidType.WorldSid, FileSystemRights.Modify, AccessControlType.Allow); } catch (Exception ex) {