Add PosixSymlinkFactory

This commit is contained in:
Cyberboss
2018-07-23 14:23:30 -04:00
parent d0c2851641
commit 89266ba5e1
3 changed files with 29 additions and 0 deletions
@@ -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);