Moved Windows-only Permission function to Sonarr.Windows

This commit is contained in:
Taloth Saldono 2019-12-24 17:18:07 +01:00
parent 8da6f7d7f4
commit d64d59ff27
5 changed files with 54 additions and 53 deletions

View File

@ -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, string user, string group);
public abstract void CopyPermissions(string sourcePath, string targetPath, bool includeOwner);
public abstract long? GetTotalSize(string path);
@ -317,42 +318,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<FileSystemAccessRule>().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))

View File

@ -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, string user, string group);
void CopyPermissions(string sourcePath, string targetPath, bool includeOwner = false);
long? GetTotalSize(string path);
@ -39,7 +40,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);

View File

@ -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)
{

View File

@ -53,20 +53,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, string user, string group)

View File

@ -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<FileSystemAccessRule>().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, string user, string group)