mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-03 04:34:24 +00:00
Moved temporary files into a folder of their own below the data directory (#1245)
This commit is contained in:
1 parent
ddfc632b63
commit
12d3e1ec6c
52 files changed
+1219
-396
No files matched your search
@@ -8,70 +8,43 @@ public sealed partial class PluginInstallService
|
||||
/// Creates a staging directory for a plugin that is about to be installed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The staging directory lives below the data directory, never below the temporary directory of
|
||||
/// the operating system. Installing means moving the staged plugin into the plugins directory,
|
||||
/// and a directory move cannot cross a file system boundary. Flatpak is the case where this
|
||||
/// always applies: its temporary directory is a tmpfs inside the sandbox, while the data
|
||||
/// directory lives in the home directory of the user.<br/><br/>
|
||||
/// The staging directory lives in the temporary folder of AI Studio below the data directory
|
||||
/// (TemporaryFiles), never below the temporary directory of the operating system. Installing means
|
||||
/// moving the staged plugin into the plugins directory, and a directory move cannot cross a file
|
||||
/// system boundary. Flatpak is the case where this always applies: its temporary directory is a
|
||||
/// tmpfs inside the sandbox, while the data directory lives in the home directory of the user.
|
||||
/// Whatever a crash leaves behind is gone with the next start of AI Studio.<br/><br/>
|
||||
/// It lives next to the plugins directory, not inside it: the plugin loader searches the plugins
|
||||
/// directory recursively, so a half-written plugin there would be loaded while it is still being
|
||||
/// staged.
|
||||
/// </remarks>
|
||||
/// <param name="prefix">The prefix of the staging directory name, naming the caller.</param>
|
||||
/// <param name="stagingDirectory">The created staging directory.</param>
|
||||
/// <param name="issue">A user-facing issue when the staging directory could not be created.</param>
|
||||
/// <returns>True when the staging directory exists, false otherwise.</returns>
|
||||
private bool TryCreateStagingDirectory(string prefix, out string stagingDirectory, out string issue)
|
||||
private bool TryCreateStagingDirectory(out string stagingDirectory, out string issue)
|
||||
{
|
||||
stagingDirectory = string.Empty;
|
||||
issue = string.Empty;
|
||||
|
||||
var dataDirectory = SettingsManager.DataDirectory;
|
||||
if (string.IsNullOrWhiteSpace(dataDirectory))
|
||||
if (string.IsNullOrWhiteSpace(SettingsManager.DataDirectory))
|
||||
{
|
||||
issue = TB("The AI Studio data directory is not initialized yet.");
|
||||
return false;
|
||||
}
|
||||
|
||||
var stagingRoot = Path.Join(dataDirectory, STAGING_DIRECTORY);
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(stagingRoot);
|
||||
this.CleanUpExpiredStagingDirectories(stagingRoot);
|
||||
|
||||
stagingDirectory = Path.Join(stagingRoot, $"{prefix}.staging-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(stagingDirectory);
|
||||
stagingDirectory = TemporaryFiles.CreateDirectory();
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.logger.LogError(e, "Failed to create the plugin staging directory below '{StagingRoot}'.", stagingRoot);
|
||||
stagingDirectory = string.Empty;
|
||||
this.logger.LogError(e, "Failed to create the plugin staging directory in the temporary folder of AI Studio.");
|
||||
issue = string.Format(TB("Unexpected error: {0}"), e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes staging directories which an earlier installation left behind, e.g. after a crash.
|
||||
/// </summary>
|
||||
private void CleanUpExpiredStagingDirectories(string stagingRoot)
|
||||
{
|
||||
var expiry = DateTime.UtcNow.AddHours(-STAGING_RETENTION_HOURS);
|
||||
foreach (var leftOverDirectory in Directory.EnumerateDirectories(stagingRoot, "*", SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Directory.GetLastWriteTimeUtc(leftOverDirectory) < expiry)
|
||||
Directory.Delete(leftOverDirectory, true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.logger.LogWarning(e, "Failed to delete the left-over plugin staging directory '{StagingDirectory}'.", leftOverDirectory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves a directory and falls back to copying it when the move crosses a file system boundary.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user