Files
Chronos/src/Titano.Setup/Engine/CommandProgram.cs
T
Claude 067e8ca062 fix(setup): change only what starts a program setup installed; Server only inside Program Files
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
2026-10-12 11:41:30 +00:00

88 lines
3.7 KiB
C#

using System.IO;
using Titano.Core.Packaging;
namespace Titano.Setup.Engine;
/// <summary>
/// The program a command line registered with Windows starts (a service's image path, a Run value), and whether it is
/// one setup installed: setup changes only what starts a program its log or payload lists, never another program
/// that happens to live in the same folder.
/// </summary>
internal static class CommandProgram
{
/// <summary>
/// The first token, quoted or up to <c>.exe</c>, with environment variables expanded; for the <c>dotnet</c> host,
/// the assembly it runs. Null when the line names no full path.
/// </summary>
public static string? Of(string commandLine)
{
var line = Environment.ExpandEnvironmentVariables(commandLine).Trim();
var first = NextToken(ref line);
if (first is null) return null;
if (Path.GetFileNameWithoutExtension(first).Equals("dotnet", StringComparison.OrdinalIgnoreCase)) first = NextToken(ref line);
return first is not null && Path.IsPathFullyQualified(first) ? first : null;
}
/// <summary>
/// Full paths of <paramref name="payloadPaths"/> (an install log's or a manifest's files) under
/// <paramref name="directory"/>, compared ignoring case: the programs setup installs or installed there.
/// </summary>
public static IReadOnlySet<string> FullPaths(string directory, IEnumerable<string> payloadPaths)
{
var paths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var path in payloadPaths.Where(PayloadPath.IsValid)) paths.Add(PayloadPath.Resolve(directory, path));
return paths;
}
/// <summary>True when <paramref name="program"/> (a full path, in any form Windows accepts) is one of <paramref name="programs"/>.</summary>
public static bool IsAmong(string? program, IReadOnlySet<string> programs)
{
if (string.IsNullOrEmpty(program) || !Path.IsPathFullyQualified(program)) return false;
try
{
return programs.Contains(Path.GetFullPath(program));
}
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
{
return false;
}
}
/// <summary><see cref="SetupPaths.IsSameOrInside"/> that answers false for a path Windows cannot normalise.</summary>
public static bool IsPathInside(string path, string directory)
{
try
{
return SetupPaths.IsSameOrInside(path, directory);
}
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
{
return false;
}
}
/// <summary>Takes the next token of a command line: quoted, or up to <c>.exe</c> followed by a space, or up to the next space.</summary>
private static string? NextToken(ref string line)
{
line = line.TrimStart();
if (line.Length == 0) return null;
string token;
if (line[0] == '"')
{
var close = line.IndexOf('"', 1);
token = close < 0 ? line[1..] : line[1..close];
line = close < 0 ? string.Empty : line[(close + 1)..];
}
else
{
// An unquoted path with spaces ("C:\Program Files\Titano\Titano.Server.exe --service") ends at its extension.
var exe = line.IndexOf(".exe", StringComparison.OrdinalIgnoreCase);
var space = line.IndexOf(' ');
var end = exe >= 0 && (exe + 4 == line.Length || line[exe + 4] == ' ') ? exe + 4 : space >= 0 ? space : line.Length;
token = line[..end];
line = line[end..];
}
return token.Length == 0 ? null : token;
}
}