Merge pull request #879 from wabbajack-tools/make-delete-async

Make all delete operations async
This commit is contained in:
Timothy Baldridge 2020-05-26 06:10:41 -07:00 committed by GitHub
commit 8a03fe7384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 53 additions and 28 deletions

View File

@ -12,7 +12,7 @@ namespace Wabbajack.Common.Test
await tempFile.Path.WriteAllTextAsync("Test");
tempFile.Path.SetReadOnly(true);
tempFile.Path.Delete();
await tempFile.Path.DeleteAsync();
}
[Fact]

View File

@ -32,6 +32,30 @@ namespace Wabbajack.Common
goto TOP;
}
}
public static async ValueTask WithAutoRetry<TE>(Func<ValueTask> f, TimeSpan? delay = null, int? multipler = null, int? maxRetries = null) where TE : Exception
{
int retries = 0;
delay ??= DEFAULT_DELAY;
multipler ??= DEFAULT_DELAY_MULTIPLIER;
maxRetries ??= DEFAULT_RETRIES;
TOP:
try
{
await f();
}
catch (TE ex)
{
retries += 1;
if (retries > maxRetries)
throw;
Utils.Log($"(Retry {retries} of {maxRetries}), got exception {ex.Message}, waiting {delay.Value.TotalMilliseconds}ms");
await Task.Delay(delay.Value);
delay = delay * multipler;
goto TOP;
}
}
}
}

View File

@ -181,10 +181,10 @@ namespace Wabbajack.Common
if (Root != otherPath.Root)
{
if (otherPath.Exists && overwrite)
otherPath.Delete();
await otherPath.DeleteAsync();
await CopyToAsync(otherPath);
Delete();
await DeleteAsync();
return;
}
File.Move(_path, otherPath._path, overwrite ? MoveOptions.ReplaceExisting : MoveOptions.None);
@ -248,13 +248,14 @@ namespace Wabbajack.Common
Directory.CreateDirectory(_path);
}
public void Delete()
public async Task DeleteAsync()
{
if (!IsFile) return;
if (IsReadOnly) IsReadOnly = false;
File.Delete(_path);
var path = _path;
await CircuitBreaker.WithAutoRetry<IOException>(async () => File.Delete(path));
}
public bool InFolder(AbsolutePath folder)

View File

@ -89,7 +89,7 @@ namespace Wabbajack.Common
{
try
{
f.Delete();
f.DeleteAsync().Wait();
success++;
}
catch (Exception e)
@ -801,7 +801,7 @@ namespace Wabbajack.Common
}
catch (UnauthorizedAccessException)
{
tmpName.Delete();
await tmpName.DeleteAsync();
}
}
@ -929,7 +929,7 @@ namespace Wabbajack.Common
size += buffer.Length;
}
}
file.Delete();
await file.DeleteAsync();
return size;
});
return results.Sum() / seconds;
@ -1086,9 +1086,9 @@ namespace Wabbajack.Common
.DistinctUntilChanged();
}
public static void DeleteEncryptedJson(string key)
public static async ValueTask DeleteEncryptedJson(string key)
{
Consts.LocalAppDataPath.Combine(key).Delete();
await Consts.LocalAppDataPath.Combine(key).DeleteAsync();
}
public static void StartProcessFromFile(string file)

View File

@ -147,7 +147,7 @@ namespace Wabbajack.Lib
using (var of = await ModListOutputFolder.Combine("modlist").Create())
ModList.ToJson(of);
ModListOutputFile.Delete();
await ModListOutputFile.DeleteAsync();
using (var fs = await ModListOutputFile.Create())
{

View File

@ -148,7 +148,7 @@ namespace Wabbajack.Lib
{
if (to.IsReadOnly)
to.IsReadOnly = false;
to.Delete();
await to.DeleteAsync();
}
if (from.Exists)
@ -210,7 +210,7 @@ namespace Wabbajack.Lib
var oldData = new MemoryStream(await toFile.ReadAllBytesAsync());
// Remove the file we're about to patch
toFile.Delete();
await toFile.DeleteAsync();
// Patch it
await using (var outStream = await toFile.Create())
@ -264,7 +264,7 @@ namespace Wabbajack.Lib
var ext = Path.GetExtension(archive.Name);
var uniqueKey = archive.State.PrimaryKeyString.StringSha256Hex();
outputPath = DownloadFolder.Combine(origName + "_" + uniqueKey + "_" + ext);
outputPath.Delete();
await outputPath.DeleteAsync();
}
}
@ -358,7 +358,7 @@ namespace Wabbajack.Lib
UpdateTracker.NextStep("Looking for files to delete");
await OutputFolder.EnumerateFiles()
.PMap(Queue, UpdateTracker, f =>
.PMap(Queue, UpdateTracker, async f =>
{
var relativeTo = f.RelativeTo(OutputFolder);
Utils.Status($"Checking if ModList file {relativeTo}");
@ -366,7 +366,7 @@ namespace Wabbajack.Lib
return;
Utils.Log($"Deleting {relativeTo} it's not part of this ModList");
f.Delete();
await f.DeleteAsync();
});
Utils.Log("Cleaning empty folders");

View File

@ -43,7 +43,7 @@ namespace Wabbajack.Lib.Downloaders
public BethesdaNetDownloader()
{
TriggerLogin = ReactiveCommand.CreateFromTask(() => Utils.CatchAndLog(RequestLoginAndCache), IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler));
ClearLogin = ReactiveCommand.Create(() => Utils.DeleteEncryptedJson(DataName), IsLoggedIn.ObserveOn(RxApp.MainThreadScheduler));
ClearLogin = ReactiveCommand.Create(() => Utils.CatchAndLog(() =>Utils.DeleteEncryptedJson(DataName)), IsLoggedIn.ObserveOn(RxApp.MainThreadScheduler));
}
private static async Task RequestLoginAndCache()

View File

@ -233,7 +233,7 @@ namespace Wabbajack.Lib
{
Status($"Writing included .meta file {directive.To}");
var outPath = DownloadFolder.Combine(directive.To);
if (outPath.IsFile) outPath.Delete();
if (outPath.IsFile) await outPath.DeleteAsync();
await outPath.WriteAllBytesAsync(await LoadBytesFromPath(directive.SourceDataID));
});
}
@ -296,7 +296,7 @@ namespace Wabbajack.Lib
{
Status($"Writing included file {directive.To}");
var outPath = OutputFolder.Combine(directive.To);
outPath.Delete();
await outPath.DeleteAsync();
switch (directive)
{

View File

@ -48,7 +48,7 @@ namespace Wabbajack.Server.Services
var hash = await file.FileHashAsync();
if (HaveArchive(hash))
{
file.Delete();
await file.DeleteAsync();
return _archives[hash];
}

View File

@ -64,7 +64,7 @@ namespace Wabbajack.Test
DownloadAndInstall(Game.SkyrimSpecialEdition, 32359, "Frost Armor HDT"));
// We're going to fully patch this mod from another source.
modfiles[3].Download.Delete();
await modfiles[3].Download.DeleteAsync();
await utils.Configure();

View File

@ -78,7 +78,7 @@ namespace Wabbajack.Test
Assert.Equal(server.Data, await testFile.Path.ReadAllBytesAsync());
testFile.Path.Delete();
await testFile.Path.DeleteAsync();
}
public void Dispose()

View File

@ -164,7 +164,7 @@ namespace Wabbajack.Test
await modifiedPath.WriteAllTextAsync("random data");
var modifiedModified = modifiedPath.LastModified;
deletedPath.Delete();
await deletedPath.DeleteAsync();
Assert.True(extraPath.Exists);

View File

@ -108,7 +108,7 @@ namespace Wabbajack.VirtualFileSystem.Test
Assert.Equal(14, file.Size);
Assert.Equal(Hash.FromBase64("qX0GZvIaTKM="), file.Hash);
TEST_TXT.Delete();
await TEST_TXT.DeleteAsync();
await AddTestRoot();

View File

@ -195,7 +195,7 @@ namespace Wabbajack.VirtualFileSystem
}
catch (IOException)
{
filename.Delete();
await filename.DeleteAsync();
}
}

View File

@ -50,10 +50,10 @@ namespace Wabbajack
}
var backup = Consts.SettingsFile.AppendToName("-backup");
backup.Delete();
await backup.DeleteAsync();
await Consts.SettingsFile.CopyToAsync(backup);
Consts.SettingsFile.Delete();
await Consts.SettingsFile.DeleteAsync();
return default;
}