Files
ThreadPilot/Services/UpdateTempDirectoryProvider.cs
T
PrimeBuild-pc 6f84ead769 fix: delete update folders left in Windows\Temp by earlier updates
A successful update exits right after starting the installer, so its ~55 MB folder was never
removed. At elevated startup, folders older than a day are deleted, only when they are owned by
Administrators (a user cannot create those or change what is inside).
2026-10-22 10:54:37 +02:00

91 lines
3.7 KiB
C#

/*
* ThreadPilot - safe temporary directory management for update downloads.
*/
namespace ThreadPilot.Services
{
using System;
using System.IO;
using System.Security.Principal;
/// <summary>
/// Each download gets its own admin-only folder directly under the root, with an unguessable name, so a
/// non-admin process cannot swap the verified installer (or plant DLLs beside it) before it runs elevated.
/// </summary>
public sealed class UpdateTempDirectoryProvider : IUpdateTempDirectoryProvider
{
private const string FolderPrefix = "ThreadPilot-update-";
private readonly string rootDirectory;
private readonly System.Collections.Concurrent.ConcurrentDictionary<string, byte> created = new(StringComparer.OrdinalIgnoreCase);
public UpdateTempDirectoryProvider()
: this(AdminOnlyDirectory.DefaultRoot)
{
}
public UpdateTempDirectoryProvider(string rootDirectory)
{
this.rootDirectory = Path.GetFullPath(rootDirectory ?? throw new ArgumentNullException(nameof(rootDirectory)));
}
public string CreateUpdateTempDirectory(SemanticVersion version)
{
var directory = AdminOnlyDirectory.Create(Path.Combine(this.rootDirectory, $"{FolderPrefix}{version}-{Guid.NewGuid():N}"));
this.created[directory] = 0;
return directory;
}
public bool IsSafeUpdateTempPath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return false;
}
// Only folders this instance created: anyone may create a ThreadPilot-update-* folder in the root.
var fullPath = Path.TrimEndingDirectorySeparator(Path.GetFullPath(path));
return this.created.Keys.Any(directory =>
string.Equals(fullPath, directory, StringComparison.OrdinalIgnoreCase)
|| fullPath.StartsWith(directory + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase));
}
public void Cleanup(string path)
{
if (!this.IsSafeUpdateTempPath(path) || !Directory.Exists(path))
{
return;
}
Directory.Delete(path, recursive: true);
this.created.TryRemove(Path.TrimEndingDirectorySeparator(Path.GetFullPath(path)), out _);
}
/// <summary>
/// Deletes update folders left by earlier runs (a successful update exits before it can clean up).
/// Only Administrators-owned folders: a user cannot create one, nor change what is inside it.
/// </summary>
public void CleanupStale(TimeSpan olderThan)
{
var administrators = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
foreach (var directory in new DirectoryInfo(this.rootDirectory).EnumerateDirectories(FolderPrefix + "*"))
{
try
{
if (directory.Attributes.HasFlag(FileAttributes.ReparsePoint)
|| DateTime.UtcNow - directory.CreationTimeUtc < olderThan
|| !administrators.Equals(directory.GetAccessControl().GetOwner(typeof(SecurityIdentifier))))
{
continue;
}
directory.Delete(recursive: true);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or System.Security.SecurityException)
{
// In use or not ours: the next start tries again.
}
}
}
}
}