ok so i messed up with the FileInfo, because it was already there and I just lazily used it. But lets try handling this with the sophistication it calls for

This commit is contained in:
zeromus 2016-02-27 16:19:33 -06:00
parent c04d813b77
commit 3c96e9d434
2 changed files with 47 additions and 19 deletions

View File

@ -3790,26 +3790,9 @@ namespace BizHawk.Client.EmuHawk
file.Directory.Create();
}
// Make backup first
if (Global.Config.BackupSavestates && file.Exists)
{
var backup = path + ".bak";
var backupFile = new FileInfo(backup);
if (backupFile.Exists)
{
backupFile.Delete();
}
try
{
File.Move(path, backup);
}
catch
{
// Eat it, this will happen rarely and the user will rarely need the file, so the odds of simply not making the backup is very unlikely
}
}
if (Global.Config.BackupSavestates)
BizHawk.Common.Util.TryMoveBackupFile(path, path + ".bak");
SaveState(path, quickSlotName, false);

View File

@ -24,6 +24,51 @@ namespace BizHawk.Common
}
}
/// <summary>
/// Tries to moves `pathWant` out of the way to `pathBackup`, delaying as needed to accomodate filesystem being sucky.
/// `pathWant` might not be removed after all, in case it's snagged by something.
/// </summary>
public static bool TryMoveBackupFile(string pathWant, string pathBackup)
{
//If the path we want is available we dont actually have to make a backup
if (!File.Exists(pathWant))
return true;
//delete any existing backup
try
{
if (File.Exists(pathBackup))
File.Delete(pathBackup);
}
catch
{
//just give up on the whole thing in case of exceptions. pathWant will get overwritten by the caller.
return false;
}
//deletes are asynchronous, need to wait for it to be gone
while (File.Exists(pathBackup))
System.Threading.Thread.Sleep(10);
try
{
//actually move pathWant out of the way to pathBackup now that pathBackup is free
File.Move(pathWant, pathBackup);
}
catch
{
// Eat it, this will happen rarely and the user will rarely need the file, so the odds of simply not making the backup is very unlikely
return false;
}
//hmm these might be asynchronous too
//wait for the move to complete, at least enough for pathWant to be cleared up
while (File.Exists(pathWant))
System.Threading.Thread.Sleep(10);
return true;
}
public static bool IsPowerOfTwo(int x)
{
if (x == 0 || x == 1)