Rollback safe file saving
MODIFY
WalletWasabi/Helpers/IoHelpers.cs
WalletWasabi/Helpers/IoHelpers.cs
@@ -75,162 +75,54 @@ namespace System.IO
File.Delete(oldPath);
}
- public static async Task SafeWriteAllTextAsync(string path, string content)
- {
- string newPath = path + NewExtension;
- string lockPath = CreateWeakLockOrDelayWhileExists(path);
- try
- {
- await File.WriteAllTextAsync(newPath, content);
- SafeMove(newPath, path);
- }
- finally
- {
- RemoveWeakLock(lockPath);
- }
- }
-
public static void SafeWriteAllText(string path, string content)
{
- string newPath = path + NewExtension;
- string lockPath = CreateWeakLockOrDelayWhileExists(path);
- try
- {
+ var newPath = path + NewExtension;
File.WriteAllText(newPath, content);
SafeMove(newPath, path);
}
- finally
- {
- RemoveWeakLock(lockPath);
- }
- }
-
- public static async Task SafeWriteAllTextAsync(string path, string content, Encoding encoding)
- {
- string newPath = path + NewExtension;
- string lockPath = CreateWeakLockOrDelayWhileExists(path);
- try
- {
- await File.WriteAllTextAsync(newPath, content, encoding);
- SafeMove(newPath, path);
- }
- finally
- {
- RemoveWeakLock(lockPath);
- }
- }
public static void SafeWriteAllText(string path, string content, Encoding encoding)
{
- string newPath = path + NewExtension;
- string lockPath = CreateWeakLockOrDelayWhileExists(path);
- try
- {
+ var newPath = path + NewExtension;
File.WriteAllText(newPath, content, encoding);
SafeMove(newPath, path);
}
- finally
- {
- RemoveWeakLock(lockPath);
- }
- }
- public static async Task SafeWriteAllLinesAsync(string path, IEnumerable<string> content)
- {
- string newPath = path + NewExtension;
- string lockPath = CreateWeakLockOrDelayWhileExists(path);
- try
+ public static async Task SafeWriteAllTextAsync(string path, string content)
{
- await File.WriteAllLinesAsync(newPath, content);
+ var newPath = path + NewExtension;
+ await File.WriteAllTextAsync(newPath, content);
SafeMove(newPath, path);
}
- finally
+
+ public static async Task SafeWriteAllTextAsync(string path, string content, Encoding encoding)
{
- RemoveWeakLock(lockPath);
- }
+ var newPath = path + NewExtension;
+ await File.WriteAllTextAsync(newPath, content, encoding);
+ SafeMove(newPath, path);
}
public static void SafeWriteAllLines(string path, IEnumerable<string> content)
{
- string newPath = path + NewExtension;
- string lockPath = CreateWeakLockOrDelayWhileExists(path);
- try
- {
+ var newPath = path + NewExtension;
File.WriteAllLines(newPath, content);
SafeMove(newPath, path);
}
- finally
- {
- RemoveWeakLock(lockPath);
- }
- }
- public static async Task SafeWriteAllBytesAsync(string path, byte[] content)
- {
- string newPath = path + NewExtension;
- string lockPath = CreateWeakLockOrDelayWhileExists(path);
- try
+ public static async Task SafeWriteAllLinesAsync(string path, IEnumerable<string> content)
{
- await File.WriteAllBytesAsync(newPath, content);
+ var newPath = path + NewExtension;
+ await File.WriteAllLinesAsync(newPath, content);
SafeMove(newPath, path);
}
- finally
- {
- RemoveWeakLock(lockPath);
- }
- }
- public static void SafeWriteAllBytes(string path, byte[] content)
- {
- string newPath = path + NewExtension;
- string lockPath = CreateWeakLockOrDelayWhileExists(path);
- try
+ public static async Task SafeWriteAllBytesAsync(string path, byte[] content)
{
- File.WriteAllBytes(newPath, content);
+ var newPath = path + NewExtension;
+ await File.WriteAllBytesAsync(newPath, content);
SafeMove(newPath, path);
}
- finally
- {
- RemoveWeakLock(lockPath);
- }
- }
-
- /// <summary>This function blocks the thread!</summary>
- /// <param name="delayTimesBeforeForceIn">Times * 100ms delay before it forces itself into the lock.</param>
- /// <returns>lock file path</returns>
- private static string CreateWeakLockOrDelayWhileExists(string path, int delayTimesBeforeForceIn = 70)
- {
- // This function is blocking, because .NET Core async brainfart.
- string lockPath = path + WeakLockExtension;
- var count = 0;
- while (File.Exists(lockPath))
- {
- Thread.Sleep(100);
- if (count > delayTimesBeforeForceIn)
- {
- return lockPath;
- }
- count++;
- }
- File.Create(lockPath);
- return lockPath;
- }
-
- private static void RemoveWeakLock(string lockPath)
- {
- try
- {
- // .NET Core brainfart
- // https://stackoverflow.com/a/18278033/2061103
- GC.Collect();
- GC.WaitForPendingFinalizers();
- File.Delete(lockPath);
- }
- catch (Exception ex)
- {
- Logger.LogDebug(ex, nameof(IoHelpers));
- }
- }
public static async Task BetterExtractZipToDirectoryAsync(string src, string dest)
{

Rollback safe file saving