Skip to content

Commit

Permalink
Create symbolic link from WingetUI\choco-cli to UniGetUI\Chocolatey o…
Browse files Browse the repository at this point in the history
…n LocalAppData when migrating (fix #2477, fix #2514)
  • Loading branch information
marticliment committed Jul 19, 2024
1 parent de9f8b3 commit df4ffa9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
46 changes: 46 additions & 0 deletions src/UniGetUI.Core.Tools/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,52 @@ public static long HashStringAsLong(string inputString)
byte[] bytes = MD5.HashData(Encoding.UTF8.GetBytes(inputString));
return BitConverter.ToInt64(bytes, 0);
}

/// <summary>
/// Creates a symbolic link between directories
/// </summary>
/// <param name="linkPath">The location of the link to be created</param>
/// <param name="targetPath">The location of the real folder where to point</param>
/// <returns></returns>
public static async Task CreateSymbolicLinkDir(string linkPath, string targetPath)
{
var psi = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c mklink /D \"{linkPath}\" \"{targetPath}\"",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};

using (var process = Process.Start(psi))
{
await process.WaitForExitAsync();

Check warning on line 455 in src/UniGetUI.Core.Tools/Tools.cs

View workflow job for this annotation

GitHub Actions / test

Dereference of a possibly null reference.
if (process.ExitCode != 0)
{
throw new Exception(
$"The operation did not complete successfully: \n{await process.StandardOutput.ReadToEndAsync()}\n{await process.StandardError.ReadToEndAsync()}\n");
}
}
}

/// <summary>
/// Will check wether the given folder is a symbolic link
/// </summary>
/// <param name="path">The folder to check</param>
/// <returns></returns>
/// <exception cref="FileNotFoundException"></exception>
public static bool IsSymbolicLinkDir(string path)
{
if (!Directory.Exists(path) && !File.Exists(path))
{
throw new FileNotFoundException("The specified path does not exist.", path);
}

var attributes = File.GetAttributes(path);
return (attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint;
}
}
}

17 changes: 14 additions & 3 deletions src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,21 @@ protected override async Task<ManagerStatus> LoadManager()
string old_choco_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs\\WingetUI\\choco-cli");
string new_choco_path = Path.Join(CoreData.UniGetUIDataDirectory, "Chocolatey");

if (Directory.Exists(old_choco_path))
if(!Directory.Exists(old_choco_path))
Logger.Debug("Old chocolatey path does not exist, not migrating Chocolatey");
else if (CoreTools.IsSymbolicLinkDir(old_choco_path))
Logger.ImportantInfo("Old chocolatey path is a symbolic link, not migrating Chocolatey...");
else if (Settings.Get("ChocolateySymbolicLinkCreated"))
Logger.Warn("The Choco path symbolic link has already been set to created!");
else
{
try
{
Logger.Info("Moving Bundled Chocolatey from old path to new path...");

if(Path.GetRelativePath(Environment.GetEnvironmentVariable("chocolateyinstall", EnvironmentVariableTarget.User) ?? "", old_choco_path) == ".")
string current_env_var =
Environment.GetEnvironmentVariable("chocolateyinstall", EnvironmentVariableTarget.User) ?? "";
if(current_env_var != "" && Path.GetRelativePath(current_env_var, old_choco_path) == ".")
{
Logger.ImportantInfo("Migrating ChocolateyInstall environment variable to new location");
Environment.SetEnvironmentVariable("chocolateyinstall", new_choco_path, EnvironmentVariableTarget.User);
Expand Down Expand Up @@ -335,7 +343,10 @@ protected override async Task<ManagerStatus> LoadManager()
Logger.Info("Deleting old Chocolatey directory " + old_choco_path);
Directory.Delete(old_choco_path);
}


await CoreTools.CreateSymbolicLinkDir(old_choco_path, new_choco_path);
Settings.Set("ChocolateySymbolicLinkCreated", true);
Logger.Info($"Symbolic link created successfully from {old_choco_path} to {new_choco_path}.");

}
catch (Exception e)
Expand Down

0 comments on commit df4ffa9

Please sign in to comment.