mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-27 15:07:03 +01:00
Add PosixSymlinkFactory
This commit is contained in:
@@ -158,6 +158,7 @@ namespace Tgstation.Server.Host.Core
|
||||
else
|
||||
{
|
||||
services.AddSingleton<ISystemIdentityFactory, PosixSystemIdentityFactory>();
|
||||
services.AddSingleton<ISymlinkFactory, PosixSymlinkFactory>();
|
||||
}
|
||||
|
||||
services.AddSingleton<IExecutor, Executor>();
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using Mono.Unix;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tgstation.Server.Host.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="ISymlinkFactory"/> for posix systems
|
||||
/// </summary>
|
||||
sealed class PosixSymlinkFactory : ISymlinkFactory
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Task CreateSymbolicLink(string targetPath, string linkPath, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
|
||||
{
|
||||
UnixFileSystemInfo fsInfo;
|
||||
var isFile = File.Exists(targetPath);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
if (isFile)
|
||||
fsInfo = new UnixFileInfo(targetPath);
|
||||
else
|
||||
fsInfo = new UnixDirectoryInfo(targetPath);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
fsInfo.CreateSymbolicLink(linkPath);
|
||||
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ namespace Tgstation.Server.Host.IO
|
||||
var flags = File.Exists(targetPath) ? 0 : 1; //SYMBOLIC_LINK_FLAG_DIRECTORY
|
||||
flags |= 2; //SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE on win10 1607+
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
if (!NativeMethods.CreateSymbolicLink(linkPath, targetPath, flags))
|
||||
throw new Win32Exception(Marshal.GetLastWin32Error());
|
||||
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
|
||||
|
||||
Reference in New Issue
Block a user