Moved temporary files into a folder of their own below the data directory (#1245)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-29 18:45:34 +01:00
1 parent ddfc632b63
commit 12d3e1ec6c
52 files changed
+1219 -396

No files matched your search

+51 -31
View File
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.ComponentModel;
using System.Diagnostics;
using System.Formats.Tar;
using System.IO.Compression;
using System.Text.RegularExpressions;
@@ -40,6 +41,16 @@ public static partial class Pandoc
/// </summary>
private static bool HAS_LOGGED_SUCCESSFUL_CHECK_ONCE;
/// <summary>
/// Tracks whether a missing Pandoc was already logged since the last check which could start it.
/// </summary>
/// <remarks>
/// Most users have no Pandoc until they install it through AI Studio. That is a stable state, not a failure, so
/// it is logged once, like <see cref="HAS_LOGGED_EXECUTABLE_ONCE"/>. A check which can start Pandoc resets it, so
/// that a Pandoc which disappears later shows up in the log again.
/// </remarks>
private static bool HAS_LOGGED_MISSING_ONCE;
private static readonly HttpClient WEB_CLIENT = new();
private static readonly SemaphoreSlim INSTALLATION_LOCK = new(1, 1);
@@ -78,6 +89,8 @@ public static partial class Pandoc
return new(false, TB("Was not able to check the Pandoc installation."), false, string.Empty, preparedProcess.IsLocal);
}
HAS_LOGGED_MISSING_ONCE = false;
// Read output streams asynchronously while the process runs (prevents deadlock):
var outputTask = process.StandardOutput.ReadToEndAsync();
var errorTask = process.StandardError.ReadToEndAsync();
@@ -138,12 +151,29 @@ public static partial class Pandoc
if (showMessages)
await MessageBus.INSTANCE.SendError(new(@AppIcons.PANDOC_UNAVAILABLE, TB("Pandoc doesn't seem to be installed.")));
LOG.LogError(e, "Pandoc availability check failed. This usually means Pandoc is not installed or not in the system PATH.");
if (!IsMissingExecutable(e))
LOG.LogError(e, "The Pandoc availability check failed, but not because Pandoc is missing.");
else if (!HAS_LOGGED_MISSING_ONCE)
{
LOG.LogInformation("Pandoc is not installed: AI Studio found it neither in its data directory nor on the system PATH.");
HAS_LOGGED_MISSING_ONCE = true;
}
return new(false, TB("Pandoc doesn't seem to be installed."), false, string.Empty, false);
}
}
/// <summary>
/// Whether starting a process failed because its executable does not exist.
/// </summary>
/// <remarks>
/// Windows reports ERROR_FILE_NOT_FOUND, macOS and Linux report ENOENT; both have the number 2. Any other failure, such as
/// a denied access or a damaged executable, means that something exists which cannot be started.
/// </remarks>
/// <param name="exception">What Process.Start threw.</param>
/// <returns>True, when the executable does not exist.</returns>
internal static bool IsMissingExecutable(Exception exception) => exception is Win32Exception { NativeErrorCode: 2 };
/// <summary>
/// Automatically decompresses the latest pandoc archive into AiStudio's data directory
/// </summary>
@@ -155,17 +185,21 @@ public static partial class Pandoc
var latestVersion = await FetchLatestVersionAsync();
var installDir = await GetPandocDataFolder(rustService);
var installParentDir = Path.GetDirectoryName(installDir) ?? Path.GetTempPath();
var stagingDir = Path.Combine(installParentDir, $"pandoc-install-{Guid.NewGuid():N}");
var pandocTempDownloadFile = Path.GetTempFileName();
string? downloadDir = null;
LOG.LogInformation("Trying to install Pandoc v{0} to '{1}'...", latestVersion, installDir);
try
{
if (!Directory.Exists(installParentDir))
Directory.CreateDirectory(installParentDir);
//
// The archive and the new Pandoc stay in the temporary folder of AI Studio until the new Pandoc is
// complete, so an interrupted installation leaves nothing next to the installation. The folder lies
// below the data directory, on the same drive as the installation, so the new Pandoc can move there:
//
downloadDir = TemporaryFiles.CreateDirectory();
var pandocTempDownloadFile = Path.Combine(downloadDir, "archive");
var stagingDir = Path.Combine(downloadDir, "pandoc");
//
// Download the latest Pandoc archive from GitHub:
//
@@ -177,7 +211,10 @@ public static partial class Pandoc
return;
}
using var response = await WEB_CLIENT.GetAsync(uri);
// Only the headers, so the archive goes to the disk instead of into memory. The time limit of the client
// then covers the headers alone, so the token keeps it for the whole download:
using var downloadTimeout = new CancellationTokenSource(WEB_CLIENT.Timeout);
using var response = await WEB_CLIENT.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, downloadTimeout.Token);
if (!response.IsSuccessStatusCode)
{
await MessageBus.INSTANCE.SendError(new(AppIcons.ERROR, TB("AI Studio couldn't install Pandoc because the archive was not found.")));
@@ -188,7 +225,7 @@ public static partial class Pandoc
// Download the archive to the temporary file:
await using (var tempFileStream = File.Create(pandocTempDownloadFile))
{
await response.Content.CopyToAsync(tempFileStream);
await response.Content.CopyToAsync(tempFileStream, downloadTimeout.Token);
await tempFileStream.FlushAsync();
}
@@ -238,10 +275,8 @@ public static partial class Pandoc
}
finally
{
TryDeleteFile(pandocTempDownloadFile);
if (Directory.Exists(stagingDir))
await TryDeleteFolderAsync(stagingDir);
if (downloadDir is not null)
await TryDeleteFolderAsync(downloadDir);
INSTALLATION_LOCK.Release();
}
@@ -337,21 +372,6 @@ public static partial class Pandoc
}
}
private static void TryDeleteFile(string path)
{
if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
return;
try
{
File.Delete(path);
}
catch (Exception ex)
{
LOG.LogWarning(ex, "Was not able to delete temporary Pandoc archive: '{Path}'.", path);
}
}
private static async Task TryDeleteFolderAsync(string path)
{
if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path))