mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 12:11:10 +00:00
Removes Mono.Posix package due to incompatibility with MacOs platforms
This commit is contained in:
1 parent
01decb27d5
commit
8f9b8fe3b8
2 files changed
+84
-39
No files matched your search
+84
-38
@@ -2,8 +2,6 @@
|
|||||||
using Lumina.Domain.Common.Enums.FileSystem;
|
using Lumina.Domain.Common.Enums.FileSystem;
|
||||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||||
using Mono.Unix;
|
|
||||||
using Mono.Unix.Native;
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
@@ -55,46 +53,94 @@ internal class FileSystemPermissionsService : IFileSystemPermissionsService // T
|
|||||||
/// <param name="path">The path for which to check the access.</param>
|
/// <param name="path">The path for which to check the access.</param>
|
||||||
/// <param name="accessMode">The access mode in which to check that path access.</param>
|
/// <param name="accessMode">The access mode in which to check that path access.</param>
|
||||||
/// <returns><see langword="true"/> if the current user has rights for the specified path and acccess mode, <see langword="false"/> otherwise.</returns>
|
/// <returns><see langword="true"/> if the current user has rights for the specified path and acccess mode, <see langword="false"/> otherwise.</returns>
|
||||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when trying to check access for a mode that is not supported</exception>
|
/// <exception cref="ArgumentOutOfRangeException">Thrown when trying to check access for a mode that is not supported.</exception>
|
||||||
private static bool CanAccessPathUnix(string path, FileAccessMode accessMode)
|
private static bool CanAccessPathUnix(string path, FileAccessMode accessMode)
|
||||||
{
|
{
|
||||||
AccessModes modes;
|
try
|
||||||
UnixFileSystemInfo fileInfo = new UnixFileInfo(path);
|
|
||||||
switch (accessMode)
|
|
||||||
{
|
{
|
||||||
case FileAccessMode.ReadProperties:
|
switch (accessMode)
|
||||||
// we would typically need execute permissions on a directory, to list its contents or view file properties
|
{
|
||||||
if (fileInfo.FileType == FileTypes.Directory)
|
case FileAccessMode.ReadProperties:
|
||||||
return fileInfo.CanAccess(AccessModes.X_OK);
|
// for properties, we only need to check if the path exists
|
||||||
// for files, just verifying existence might be sufficient for reading properties
|
return File.Exists(path) || Directory.Exists(path);
|
||||||
return fileInfo.Exists;
|
case FileAccessMode.ReadContents:
|
||||||
case FileAccessMode.ReadContents:
|
// attempt to open the file for reading to verify read access
|
||||||
return fileInfo.CanAccess(AccessModes.R_OK);
|
if (File.Exists(path))
|
||||||
case FileAccessMode.Write:
|
{
|
||||||
modes = AccessModes.W_OK;
|
FileStream fileStream = File.OpenRead(path);
|
||||||
break;
|
fileStream.Dispose();
|
||||||
case FileAccessMode.Execute:
|
return true;
|
||||||
modes = AccessModes.X_OK;
|
}
|
||||||
break;
|
return false;
|
||||||
case FileAccessMode.ListDirectory:
|
case FileAccessMode.Write:
|
||||||
modes = AccessModes.F_OK; // check existence, not an exact match but the closest
|
// attempt to open the file for writing or verify directory exists
|
||||||
break;
|
if (File.Exists(path))
|
||||||
case FileAccessMode.Delete:
|
{
|
||||||
if (fileInfo.FileType == FileTypes.Directory)
|
FileStream fileStream = File.OpenWrite(path);
|
||||||
return fileInfo.CanAccess(AccessModes.W_OK | AccessModes.X_OK); // to delete a directory, we need write and execute permissions on the directory itself
|
fileStream.Dispose();
|
||||||
else
|
return true;
|
||||||
{
|
}
|
||||||
// to delete a file, we need write permission on the parent directory
|
return Directory.Exists(path);
|
||||||
string? parentDirectoryPath = Path.GetDirectoryName(path);
|
case FileAccessMode.Execute:
|
||||||
if (string.IsNullOrEmpty(parentDirectoryPath))
|
// check Unix execute permission bit
|
||||||
return false; // if path is a root directory or has no parent, deletion is not possible
|
FileInfo fileInfo = new(path);
|
||||||
UnixDirectoryInfo parentDirectoryInfo = new(parentDirectoryPath);
|
return (fileInfo.UnixFileMode & UnixFileMode.UserExecute) != 0;
|
||||||
return parentDirectoryInfo.CanAccess(AccessModes.W_OK);
|
case FileAccessMode.ListDirectory:
|
||||||
}
|
// attempt to list directory contents to verify access
|
||||||
default:
|
if (Directory.Exists(path))
|
||||||
throw new ArgumentOutOfRangeException(nameof(accessMode), "Unknown FileAccessMode");
|
{
|
||||||
|
string[] _ = Directory.GetFiles(path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
case FileAccessMode.Delete:
|
||||||
|
if (File.Exists(path))
|
||||||
|
{
|
||||||
|
// for files, check if parent directory is writable
|
||||||
|
string? directoryPath = Path.GetDirectoryName(path);
|
||||||
|
return directoryPath != null && HasWriteAccessToDirectory(directoryPath);
|
||||||
|
}
|
||||||
|
// for directories, check if the directory itself is writable
|
||||||
|
return Directory.Exists(path) && HasWriteAccessToDirectory(path);
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(accessMode), "Unknown FileAccessMode");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (UnauthorizedAccessException)
|
||||||
|
{
|
||||||
|
return false; // access denied by the operating system
|
||||||
|
}
|
||||||
|
catch (IOException)
|
||||||
|
{
|
||||||
|
return false; // IO operation failed, indicating no access
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies write access to a directory by attempting to create and delete a temporary file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">The directory path to check for write access.</param>
|
||||||
|
/// <returns><see langword="true"/> if write access is granted, <see langword="false"/> otherwise.</returns>
|
||||||
|
private static bool HasWriteAccessToDirectory(string path)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// create a unique temporary filename
|
||||||
|
string temporaryFilePath = Path.Combine(path, string.Format(".test_{0}", Guid.NewGuid()));
|
||||||
|
// attempt to create and immediately delete a temporary file
|
||||||
|
FileStream testStream = File.Create(temporaryFilePath);
|
||||||
|
testStream.Dispose();
|
||||||
|
File.Delete(temporaryFilePath);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (UnauthorizedAccessException)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
catch (IOException)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return new UnixDirectoryInfo(path).CanAccess(modes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -18,7 +18,6 @@
|
|||||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
|
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
|
||||||
<PackageReference Include="System.IO.Abstractions" Version="21.0.29" />
|
<PackageReference Include="System.IO.Abstractions" Version="21.0.29" />
|
||||||
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
|
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
|
||||||
<PackageReference Include="Mono.Posix.NETStandard" Version="5.20.1-preview" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
Reference in new issue
Block a user