1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Moved Windows-only Permission function to Radarr.Windows

Co-Authored-By: Taloth <Taloth@users.noreply.github.com>
This commit is contained in:
Qstick 2020-09-05 22:42:51 -04:00
parent 95a30b50fa
commit c1b6917afe
5 changed files with 53 additions and 58 deletions

View File

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

View File

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

View File

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

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