Remove IoHelpers with zero references
MODIFY
WalletWasabi/Helpers/IoHelpers.cs
WalletWasabi/Helpers/IoHelpers.cs
@@ -61,136 +61,6 @@ namespace System.IO
// depending on your use case, consider throwing an exception here
}
- // https://stackoverflow.com/a/7957634/2061103
- private static void SafeMove(string newPath, string path)
- {
- var oldPath = path + OldExtension;
- if (File.Exists(oldPath))
- {
- File.Delete(oldPath);
- }
-
- if (File.Exists(path))
- {
- File.Move(path, oldPath);
- }
-
- File.Move(newPath, path);
-
- File.Delete(oldPath);
- }
-
- public static void SafeWriteAllText(string path, string content)
- {
- var newPath = path + NewExtension;
- var mutexName = $"Global\\4AA0E5A2-A94F-4B92-B962-F2BBC7A68323-{Path.GetFileNameWithoutExtension(path)}";
- using (var mutex = new Mutex(false, mutexName))
- {
- var mutexAcquired = false;
- try
- {
- // acquire the mutex (or timeout after 60 seconds)
- // will return false if it timed out
- mutexAcquired = mutex.WaitOne(60000);
- }
- catch (AbandonedMutexException)
- {
- // abandoned mutexes are still acquired, we just need
- // to handle the exception and treat it as acquisition
- mutexAcquired = true;
- }
-
- if (mutexAcquired == false)
- {
- throw new IOException("Couldn't acquire Mutex on the file.");
- }
-
- try
- {
- File.WriteAllText(newPath, content);
- SafeMove(newPath, path);
- }
- finally
- {
- mutex.ReleaseMutex();
- }
- }
- }
-
- public static void SafeWriteAllText(string path, string content, Encoding encoding)
- {
- var newPath = path + NewExtension;
- var mutexName = $"Global\\4AA0E5A2-A94F-4B92-B962-F2BBC7A68323-{Path.GetFileNameWithoutExtension(path)}";
- using (var mutex = new Mutex(false, mutexName))
- {
- var mutexAcquired = false;
- try
- {
- // acquire the mutex (or timeout after 60 seconds)
- // will return false if it timed out
- mutexAcquired = mutex.WaitOne(60000);
- }
- catch (AbandonedMutexException)
- {
- // abandoned mutexes are still acquired, we just need
- // to handle the exception and treat it as acquisition
- mutexAcquired = true;
- }
-
- if (mutexAcquired == false)
- {
- throw new IOException("Couldn't acquire Mutex on the file.");
- }
-
- try
- {
- File.WriteAllText(newPath, content, encoding);
- SafeMove(newPath, path);
- }
- finally
- {
- mutex.ReleaseMutex();
- }
- }
- }
-
- public static void SafeWriteAllLines(string path, IEnumerable<string> content)
- {
- var newPath = path + NewExtension;
- var mutexName = $"Global\\4AA0E5A2-A94F-4B92-B962-F2BBC7A68323-{Path.GetFileNameWithoutExtension(path)}";
- using (var mutex = new Mutex(false, mutexName))
- {
- var mutexAcquired = false;
- try
- {
- // acquire the mutex (or timeout after 60 seconds)
- // will return false if it timed out
- mutexAcquired = mutex.WaitOne(60000);
- }
- catch (AbandonedMutexException)
- {
- // abandoned mutexes are still acquired, we just need
- // to handle the exception and treat it as acquisition
- mutexAcquired = true;
- }
-
- if (mutexAcquired == false)
- {
- throw new IOException("Couldn't acquire Mutex on the file.");
- }
-
- try
- {
- File.WriteAllLines(newPath, content);
- SafeMove(newPath, path);
- }
- finally
- {
- mutex.ReleaseMutex();
- }
- }
- }
-
public static async Task BetterExtractZipToDirectoryAsync(string src, string dest)
{
try
@@ -204,43 +74,6 @@ namespace System.IO
}
}
- // https://stackoverflow.com/a/7957634/2061103
- public static bool TryGetSafestFileVersion(string path, out string safestFilePath)
- {
- var newPath = path + NewExtension;
- var oldPath = path + OldExtension;
-
- // If foo.data and foo.data.new exist, load foo.data; foo.data.new may be broken (e.g. power off during write).
- if (File.Exists(path) && File.Exists(newPath))
- {
- safestFilePath = path;
- return true;
- }
-
- // If foo.data.old and foo.data.new exist, both should be valid, but something died very shortly afterwards - you may want to load the foo.data.old version anyway.
- if (File.Exists(oldPath) && File.Exists(newPath))
- {
- safestFilePath = oldPath;
- return true;
- }
-
- // If foo.data and foo.data.old exist, then foo.data should be fine, but again something went wrong, or possibly the file couldn't be deleted.
- if (File.Exists(oldPath) && File.Exists(path))
- {
- safestFilePath = path;
- return true;
- }
-
- if (File.Exists(path))
- {
- safestFilePath = path;
- return true;
- }
-
- safestFilePath = null;
- return false;
- }
-
public static void EnsureContainingDirectoryExists(string fileNameOrPath)
{
string fullPath = Path.GetFullPath(fileNameOrPath); // No matter if relative or absolute path is given to this.
@@ -259,19 +92,6 @@ namespace System.IO
}
}
- public static void BetterDelete(string fullPath)
- {
- if (string.IsNullOrWhiteSpace(fullPath))
- {
- return;
- }
-
- if (File.Exists(fullPath))
- {
- File.Delete(fullPath);
- }
- }
-
public static byte[] GetHashFile(string filePath)
{
var bytes = File.ReadAllBytes(filePath);

MODIFY
WalletWasabi/KeyManagement/KeyManager.cs
WalletWasabi/KeyManagement/KeyManager.cs
@@ -267,12 +267,12 @@ namespace WalletWasabi.KeyManagement
{
filePath = Guard.NotNullOrEmptyOrWhitespace(nameof(filePath), filePath);
- if (!IoHelpers.TryGetSafestFileVersion(filePath, out var safestFile))
+ if (!File.Exists(filePath))
{
throw new FileNotFoundException($"Wallet file not found at: `{filePath}`.");
}
- string jsonString = File.ReadAllText(safestFile, Encoding.UTF8);
+ string jsonString = File.ReadAllText(filePath, Encoding.UTF8);
var km = JsonConvert.DeserializeObject<KeyManager>(jsonString);
km.SetFilePath(filePath);

Remove IoHelpers with zero references