Services, sign-in entries and shortcuts are matched against the full paths of the files the install log or the payload lists, never against everything under the install folder: an install into a shared folder such as D:\Apps or /DIR="C:\Program Files" no longer stops, starts or deletes other vendors' services or removes their Run values. The elevated step now takes the names the unelevated setup found (/SERVICES=... /NAMES=...) and reads each service again, acting only when it still starts a program of the folder's log or of its own payload (or, for Prune, a program of the folder that is gone). Programs are asked to close before any service is touched. On uninstall the services are deleted only once the programs have closed, just before the files go; a failed install or uninstall starts again the services it stopped, and after an upgrade setup starts exactly the services it stopped, whatever their start type. The Server component installs only for all users in a folder inside Program Files. In the window a custom folder outside it is invalid while the Server is selected, with a tooltip saying why; silent mode refuses it. An upgrade of an installation elsewhere cannot add the Server, and one it carried is dropped with its service. Sign-in entries use StartupCommand (the same file as on fix/shell-notify) and are removed by SetupRegistry.DeleteStartupEntries only when the client's or the Uploader's value starts a program the install log lists; an upgrade that drops a program removes its entry too. StartupEntries is gone. A shortcut that starts another product's existing copy is still left alone but now logged; uninstalling the product it starts hands it to another product of the same scope that lists it and has the same program, so the Uploader that stays keeps its shortcuts. Another registered Titano installation whose folder or log cannot be read now counts as using every program's data, so "Remove my data" never offers it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
170 lines
8.4 KiB
C#
170 lines
8.4 KiB
C#
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using Titano.Core.Diagnostics;
|
|
using Titano.Core.Packaging;
|
|
using Titano.Setup.Cli;
|
|
using Titano.Setup.Interop;
|
|
|
|
namespace Titano.Setup.Engine;
|
|
|
|
/// <summary>An installation found on disk: its folder and log.</summary>
|
|
internal sealed record InstalledProduct(string Directory, InstallLog Log);
|
|
|
|
/// <summary>
|
|
/// Removes an installation using only its log: shortcuts that still start its programs (handed to another Titano
|
|
/// product that installed the same program and shares them), file types, logged files, the log, the Add/Remove
|
|
/// Programs entry and the sign-in entries of its programs, then the uninstaller itself. Files the log does not list,
|
|
/// and anything that starts a program it does not list, are left alone; user data is deleted only through a plan the
|
|
/// user saw and chose in the window. The Windows services of its programs are stopped and deleted by the caller first
|
|
/// (<see cref="InstalledServices.ApplyAsync"/>), as that may need an elevated step.
|
|
/// </summary>
|
|
internal static class UninstallEngine
|
|
{
|
|
/// <summary>The installation to remove: <c>/DIR</c>, the uninstaller's own folder, or the registered location.</summary>
|
|
public static InstalledProduct? Locate(string? directory, string? productFromPayload)
|
|
{
|
|
var candidates = new List<string?> { directory, Path.GetDirectoryName(Environment.ProcessPath) };
|
|
if (productFromPayload is not null) candidates.Add(SetupRegistry.FindInstall(SetupPaths.ProductKey(productFromPayload))?.Directory);
|
|
foreach (var candidate in candidates)
|
|
{
|
|
if (string.IsNullOrEmpty(candidate)) continue;
|
|
if (InstallLog.TryLoad(candidate) is { } log) return new InstalledProduct(Path.GetFullPath(candidate), log);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <param name="userData">The "Remove my data" plan the user confirmed; null keeps the user data.</param>
|
|
public static void Uninstall(InstalledProduct product, UserDataPlan? userData, IProgress<SetupProgress>? progress)
|
|
{
|
|
var (directory, log) = product;
|
|
Log.Info("setup", $"Removing {log.Product} {log.Version} from '{directory}' (remove data: {userData is not null})");
|
|
// First, so that a failure here leaves the program installed and the uninstaller able to retry.
|
|
if (userData is { Entries.Count: > 0 })
|
|
{
|
|
Log.Info("setup", $"Removing {userData.Entries.Count} entries of '{userData.Folder}' at the user's request");
|
|
userData.Remove();
|
|
}
|
|
if (log.Shortcuts.Count > 0)
|
|
{
|
|
var others = SetupRegistry.OtherInstalls(directory, InstallEngine.Publisher);
|
|
foreach (var link in log.Shortcuts) DeleteShortcut(link, log.Scope, directory, others);
|
|
}
|
|
progress?.Report(new SetupProgress(0.1, string.Empty));
|
|
|
|
var fileTypes = log.Associations.Where(a => SetupRegistry.IsValidFileType(a.Extension, a.ProgId)).ToList();
|
|
foreach (var a in fileTypes) SetupRegistry.UnregisterFileType(log.Scope, a.Extension, a.ProgId);
|
|
if (fileTypes.Count > 0) NativeMethods.NotifyAssociationsChanged();
|
|
progress?.Report(new SetupProgress(0.2, string.Empty));
|
|
|
|
var failed = InstalledFiles.Remove(directory, log.Files.Where(PayloadPath.IsValid));
|
|
progress?.Report(new SetupProgress(0.8, string.Empty));
|
|
if (failed.Count > 0)
|
|
{
|
|
// Keep the entry and a log of what is left, so the user can run the uninstaller again.
|
|
log.Files = [.. failed];
|
|
log.Shortcuts = [];
|
|
log.Associations = [];
|
|
log.Save(directory);
|
|
throw new SetupException(SetupExitCode.Failed, $"{failed.Count} file(s) could not be removed.");
|
|
}
|
|
|
|
SetupRegistry.DeleteUninstallEntry(log.Scope, log.ProductKey);
|
|
try
|
|
{
|
|
SetupRegistry.DeleteStartupEntries(CommandProgram.FullPaths(directory, log.Files));
|
|
}
|
|
catch (Exception ex) when (ex is System.Security.SecurityException or UnauthorizedAccessException or IOException)
|
|
{
|
|
// Windows skips a sign-in entry whose program is gone; not worth failing the removal.
|
|
Log.Warning("setup", "Sign-in entries not removed", ex);
|
|
}
|
|
File.Delete(Path.Combine(directory, InstallLog.FileName));
|
|
var uninstaller = Path.Combine(directory, log.UninstallerName);
|
|
try
|
|
{
|
|
if (Environment.ProcessPath is { } self && Path.GetFullPath(self).Equals(Path.GetFullPath(uninstaller), StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
SelfCleanup.Schedule(uninstaller);
|
|
}
|
|
else
|
|
{
|
|
if (File.Exists(uninstaller)) File.Delete(uninstaller);
|
|
InstalledFiles.Remove(directory, [], removeEmptyRoot: true);
|
|
}
|
|
}
|
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or System.ComponentModel.Win32Exception)
|
|
{
|
|
// Everything else is gone; a leftover uninstaller is not worth failing the removal.
|
|
Log.Warning("setup", $"Uninstaller '{uninstaller}' not removed", ex);
|
|
}
|
|
progress?.Report(new SetupProgress(1, string.Empty));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a shortcut recorded in a log, but only a <c>.lnk</c> directly in the Start menu or desktop folder that
|
|
/// still starts a program inside <paramref name="directory"/>: another product installing a program of the same
|
|
/// name may have pointed it at its own copy since. When another installation (<paramref name="others"/>) shares
|
|
/// the shortcut and has the same program, the shortcut is pointed at that copy instead, so the program that stays
|
|
/// installed keeps its shortcut.
|
|
/// </summary>
|
|
public static void DeleteShortcut(string link, InstallScope scope, string directory, IReadOnlyList<OtherInstall> others)
|
|
{
|
|
var folder = Path.GetDirectoryName(link);
|
|
var allowed = folder is not null && link.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase)
|
|
&& (folder.Equals(SetupPaths.StartMenu(scope), StringComparison.OrdinalIgnoreCase)
|
|
|| folder.Equals(SetupPaths.Desktop(scope), StringComparison.OrdinalIgnoreCase));
|
|
if (!allowed)
|
|
{
|
|
Log.Warning("setup", $"Ignoring shortcut outside the shortcut folders: '{link}'");
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
if (ShellLink.TargetOf(link) is not { } target) return;
|
|
if (!CommandProgram.IsPathInside(target, directory))
|
|
{
|
|
Log.Info("setup", $"Shortcut '{link}' now starts '{target}'; kept");
|
|
return;
|
|
}
|
|
if (SharedCopy(link, scope, directory, target, others) is { } copy)
|
|
{
|
|
ShellLink.Create(link, copy, Path.GetFileNameWithoutExtension(link));
|
|
Log.Info("setup", $"Shortcut '{link}' now starts '{copy}', which stays installed");
|
|
return;
|
|
}
|
|
File.Delete(link);
|
|
}
|
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or COMException)
|
|
{
|
|
Log.Warning("setup", $"Shortcut '{link}' not removed", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The same program as <paramref name="target"/> (the same path inside its folder) installed by another product of
|
|
/// the same scope whose log lists <paramref name="link"/> too; null when there is none.
|
|
/// </summary>
|
|
private static string? SharedCopy(string link, InstallScope scope, string directory, string target, IReadOnlyList<OtherInstall> others)
|
|
{
|
|
string relative;
|
|
try
|
|
{
|
|
relative = PayloadPath.FromRelative(Path.GetRelativePath(directory, target));
|
|
}
|
|
catch (Exception ex) when (ex is PayloadException or ArgumentException)
|
|
{
|
|
return null;
|
|
}
|
|
foreach (var other in others)
|
|
{
|
|
if (other is not { Directory: { } otherDirectory, Log: { } log } || log.Scope != scope) continue;
|
|
if (!log.Shortcuts.Contains(link, StringComparer.OrdinalIgnoreCase) || !log.Files.Contains(relative, PayloadPath.Comparer)) continue;
|
|
// The folder comes from another product's registry entry: one Windows cannot use is skipped.
|
|
if (!SetupPaths.IsUsableInstallDirectory(otherDirectory)) continue;
|
|
var copy = PayloadPath.Resolve(otherDirectory, relative);
|
|
if (File.Exists(copy)) return copy;
|
|
}
|
|
return null;
|
|
}
|
|
}
|