mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-15 01:48:27 +00:00
Improved how AI Studio saves its files (#1201)
This commit is contained in:
1 parent
b7255694cd
commit
dc97db256e
24 files changed
+626
-258
No files matched your search
@@ -452,33 +452,13 @@ public sealed class SettingsManager
|
||||
}
|
||||
|
||||
//
|
||||
// We write the new settings next to the previous ones and replace them afterwards, so that
|
||||
// no crash can leave a half-written settings file behind. The temporary file has to live in
|
||||
// the configuration directory for that: replacing a file is a rename, and a rename across a
|
||||
// file system boundary falls back to copying, which is exactly what we want to avoid. The
|
||||
// temporary directory of the operating system is such another file system under Flatpak.
|
||||
// AtomicFile writes the new settings next to the previous ones and replaces them afterwards,
|
||||
// so that no crash can leave a half-written settings file behind. The settings also have to
|
||||
// reach the disk first: the backup file is written at the same moment, so after a power
|
||||
// failure, it would be no better off than the settings file, and falling back to it blocks
|
||||
// storing the settings anyway.
|
||||
//
|
||||
var tempFile = $"{settingsPath}.tmp-{Guid.NewGuid():N}";
|
||||
try
|
||||
{
|
||||
await File.WriteAllTextAsync(tempFile, settingsJson);
|
||||
File.Move(tempFile, settingsPath, true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(tempFile))
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
catch (Exception cleanupException)
|
||||
{
|
||||
this.logger.LogWarning(cleanupException, $"Failed to delete the temporary settings file '{tempFile}'.");
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
await AtomicFile.WriteTextAsync(settingsPath, settingsJson, new UTF8Encoding(false), CancellationToken.None, flushToDisk: true, logger: this.logger);
|
||||
this.logger.LogInformation($"Stored the settings to '{settingsPath}'.");
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user