Moved Windows-only Permission function to Radarr.Windows

Co-Authored-By: Taloth <Taloth@users.noreply.github.com>
pull/2/head
Qstick 4 years ago
parent 95a30b50fa
commit c1b6917afe

@ -2,8 +2,6 @@ using System;
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 @@ 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);
@ -346,43 +345,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))

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

@ -65,7 +65,7 @@ namespace NzbDrone.Common.EnvironmentInfo
{
try
{
_diskProvider.SetPermissions(_appFolderInfo.AppDataFolder, WellKnownSidType.WorldSid, FileSystemRights.Modify, AccessControlType.Allow);
_diskProvider.SetEveryonePermissions(_appFolderInfo.AppDataFolder);
}
catch (Exception ex)
{

@ -53,21 +53,10 @@ namespace NzbDrone.Mono.Disk
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)

@ -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 @@ namespace NzbDrone.Windows.Disk
{
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)

Loading…
Cancel
Save