Skip to content

Commit df4ffa9

Browse files
committed
Create symbolic link from WingetUI\choco-cli to UniGetUI\Chocolatey on LocalAppData when migrating (fix #2477, fix #2514)
1 parent de9f8b3 commit df4ffa9

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

src/UniGetUI.Core.Tools/Tools.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,52 @@ public static long HashStringAsLong(string inputString)
431431
byte[] bytes = MD5.HashData(Encoding.UTF8.GetBytes(inputString));
432432
return BitConverter.ToInt64(bytes, 0);
433433
}
434+
435+
/// <summary>
436+
/// Creates a symbolic link between directories
437+
/// </summary>
438+
/// <param name="linkPath">The location of the link to be created</param>
439+
/// <param name="targetPath">The location of the real folder where to point</param>
440+
/// <returns></returns>
441+
public static async Task CreateSymbolicLinkDir(string linkPath, string targetPath)
442+
{
443+
var psi = new ProcessStartInfo
444+
{
445+
FileName = "cmd.exe",
446+
Arguments = $"/c mklink /D \"{linkPath}\" \"{targetPath}\"",
447+
UseShellExecute = false,
448+
CreateNoWindow = true,
449+
RedirectStandardOutput = true,
450+
RedirectStandardError = true
451+
};
452+
453+
using (var process = Process.Start(psi))
454+
{
455+
await process.WaitForExitAsync();
456+
if (process.ExitCode != 0)
457+
{
458+
throw new Exception(
459+
$"The operation did not complete successfully: \n{await process.StandardOutput.ReadToEndAsync()}\n{await process.StandardError.ReadToEndAsync()}\n");
460+
}
461+
}
462+
}
463+
464+
/// <summary>
465+
/// Will check wether the given folder is a symbolic link
466+
/// </summary>
467+
/// <param name="path">The folder to check</param>
468+
/// <returns></returns>
469+
/// <exception cref="FileNotFoundException"></exception>
470+
public static bool IsSymbolicLinkDir(string path)
471+
{
472+
if (!Directory.Exists(path) && !File.Exists(path))
473+
{
474+
throw new FileNotFoundException("The specified path does not exist.", path);
475+
}
476+
477+
var attributes = File.GetAttributes(path);
478+
return (attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint;
479+
}
434480
}
435481
}
436482

src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,21 @@ protected override async Task<ManagerStatus> LoadManager()
275275
string old_choco_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs\\WingetUI\\choco-cli");
276276
string new_choco_path = Path.Join(CoreData.UniGetUIDataDirectory, "Chocolatey");
277277

278-
if (Directory.Exists(old_choco_path))
278+
if(!Directory.Exists(old_choco_path))
279+
Logger.Debug("Old chocolatey path does not exist, not migrating Chocolatey");
280+
else if (CoreTools.IsSymbolicLinkDir(old_choco_path))
281+
Logger.ImportantInfo("Old chocolatey path is a symbolic link, not migrating Chocolatey...");
282+
else if (Settings.Get("ChocolateySymbolicLinkCreated"))
283+
Logger.Warn("The Choco path symbolic link has already been set to created!");
284+
else
279285
{
280286
try
281287
{
282288
Logger.Info("Moving Bundled Chocolatey from old path to new path...");
283289

284-
if(Path.GetRelativePath(Environment.GetEnvironmentVariable("chocolateyinstall", EnvironmentVariableTarget.User) ?? "", old_choco_path) == ".")
290+
string current_env_var =
291+
Environment.GetEnvironmentVariable("chocolateyinstall", EnvironmentVariableTarget.User) ?? "";
292+
if(current_env_var != "" && Path.GetRelativePath(current_env_var, old_choco_path) == ".")
285293
{
286294
Logger.ImportantInfo("Migrating ChocolateyInstall environment variable to new location");
287295
Environment.SetEnvironmentVariable("chocolateyinstall", new_choco_path, EnvironmentVariableTarget.User);
@@ -335,7 +343,10 @@ protected override async Task<ManagerStatus> LoadManager()
335343
Logger.Info("Deleting old Chocolatey directory " + old_choco_path);
336344
Directory.Delete(old_choco_path);
337345
}
338-
346+
347+
await CoreTools.CreateSymbolicLinkDir(old_choco_path, new_choco_path);
348+
Settings.Set("ChocolateySymbolicLinkCreated", true);
349+
Logger.Info($"Symbolic link created successfully from {old_choco_path} to {new_choco_path}.");
339350

340351
}
341352
catch (Exception e)

0 commit comments

Comments
 (0)