Handle create link access dennied on Windows
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Alma.Configuration.Module;
|
||||
using Alma.Configuration.Repository;
|
||||
using Alma.Data;
|
||||
@@ -86,21 +87,17 @@ public class LinkCommand : ICommand
|
||||
moduleConfiguration)).ToList();
|
||||
if (moduleConfigurationFile is not null) itemsToLink.RemoveAll(i => i.SourcePath == moduleConfigurationFileFullPath);
|
||||
|
||||
foreach (var itemToLink in itemsToLink)
|
||||
{
|
||||
Console.WriteLine($"Link: '{itemToLink.SourcePath}' '{itemToLink.TargetPath}'");
|
||||
}
|
||||
|
||||
var successfulLinks = CreateLinks(itemsToLink);
|
||||
|
||||
await _metadataHandler.SaveLinkedItemsAsync(successfulLinks, moduleDir, currentTargetDirectory);
|
||||
//await _metadataHandler.SaveLinkedItemsAsync(itemsToLink, moduleDir, currentTargetDirectory);
|
||||
}
|
||||
|
||||
private List<ItemToLink> CreateLinks(List<ItemToLink> itemsToLink)
|
||||
{
|
||||
var successfulLinks = new List<ItemToLink>();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var itemToLink in itemsToLink)
|
||||
{
|
||||
if (File.Exists(itemToLink.TargetPath) || Directory.Exists(itemToLink.TargetPath))
|
||||
@@ -112,13 +109,15 @@ public class LinkCommand : ICommand
|
||||
var sourceFileExists = File.Exists(itemToLink.SourcePath);
|
||||
var sourceDirectoryExists = Directory.Exists(itemToLink.SourcePath);
|
||||
|
||||
Console.WriteLine($"Linking: '{itemToLink.SourcePath}' '{itemToLink.TargetPath}'");
|
||||
|
||||
if (sourceFileExists)
|
||||
{
|
||||
File.CreateSymbolicLink(itemToLink.TargetPath, itemToLink.SourcePath);
|
||||
}
|
||||
else if (sourceDirectoryExists)
|
||||
{
|
||||
File.CreateSymbolicLink(itemToLink.TargetPath, itemToLink.SourcePath);
|
||||
Directory.CreateSymbolicLink(itemToLink.TargetPath, itemToLink.SourcePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -128,6 +127,15 @@ public class LinkCommand : ICommand
|
||||
|
||||
successfulLinks.Add(itemToLink);
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
Console.WriteLine("An error occured while creating links: " + e.Message);
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
Console.WriteLine("On Windows symlinks can be greated only with Administrator privileges.");
|
||||
}
|
||||
}
|
||||
|
||||
return successfulLinks;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user