Fix more warnings

This commit is contained in:
Halgari 2022-10-07 15:02:16 -06:00
parent 6bf098668a
commit 140090cfc4
5 changed files with 37 additions and 11 deletions

View File

@ -133,7 +133,7 @@ public class ModListHarness
public async Task AddManualDownload(AbsolutePath path) public async Task AddManualDownload(AbsolutePath path)
{ {
var toPath = path.FileName.RelativeTo(_downloadPath); var toPath = path.FileName.RelativeTo(_downloadPath);
await path.CopyToAsync(toPath, true, CancellationToken.None); await path.CopyToAsync(toPath, CancellationToken.None);
await toPath.WithExtension(Ext.Meta) await toPath.WithExtension(Ext.Meta)
.WriteAllLinesAsync(new[] {"[General]", $"manualURL={path.FileName}"}, CancellationToken.None); .WriteAllLinesAsync(new[] {"[General]", $"manualURL={path.FileName}"}, CancellationToken.None);
@ -170,7 +170,7 @@ public record Mod(RelativePath Name, AbsolutePath FullPath, ModListHarness Harne
public async Task<AbsolutePath> AddFile(AbsolutePath src) public async Task<AbsolutePath> AddFile(AbsolutePath src)
{ {
var dest = FullPath.Combine(src.FileName); var dest = FullPath.Combine(src.FileName);
await src.CopyToAsync(dest, true, CancellationToken.None); await src.CopyToAsync(dest, CancellationToken.None);
return dest; return dest;
} }

View File

@ -146,7 +146,7 @@ public class CompilerSanityTests : IAsyncLifetime
{ {
var newPath = file.RelativeTo(_mod.FullPath).RelativeTo(_mod.FullPath.Combine("duplicates")); var newPath = file.RelativeTo(_mod.FullPath).RelativeTo(_mod.FullPath.Combine("duplicates"));
newPath.Parent.CreateDirectory(); newPath.Parent.CreateDirectory();
await file.CopyToAsync(newPath, true, CancellationToken.None); await file.CopyToAsync(newPath, CancellationToken.None);
} }
await CompileAndValidate(5); await CompileAndValidate(5);

View File

@ -28,7 +28,7 @@ public static class AbsolutePathExtensions
{ {
File.Delete(path); File.Delete(path);
} }
catch (UnauthorizedAccessException ex) catch (UnauthorizedAccessException)
{ {
var fi = new FileInfo(path); var fi = new FileInfo(path);
if (fi.IsReadOnly) if (fi.IsReadOnly)
@ -192,7 +192,7 @@ public static class AbsolutePathExtensions
File.Move(srcStr, destStr, overwrite); File.Move(srcStr, destStr, overwrite);
return; return;
} }
catch (Exception ex) catch (Exception)
{ {
if (retries > 10) if (retries > 10)
throw; throw;
@ -202,11 +202,12 @@ public static class AbsolutePathExtensions
} }
} }
public static async ValueTask CopyToAsync(this AbsolutePath src, AbsolutePath dest, bool overwrite, public static async ValueTask CopyToAsync(this AbsolutePath src, AbsolutePath dest,
CancellationToken token) CancellationToken token)
{ {
// TODO: Make this async await using var inf = src.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
File.Copy(src.ToString(), dest.ToString(), overwrite); await using var ouf = dest.Open(FileMode.Create, FileAccess.Write, FileShare.Read);
await inf.CopyToAsync(ouf, token);
} }
public static void WriteAllText(this AbsolutePath file, string str) public static void WriteAllText(this AbsolutePath file, string str)

View File

@ -1,10 +1,11 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
namespace Wabbajack.Paths.IO; namespace Wabbajack.Paths.IO;
public class TemporaryFileManager : IDisposable public class TemporaryFileManager : IDisposable, IAsyncDisposable
{ {
private readonly AbsolutePath _basePath; private readonly AbsolutePath _basePath;
private readonly bool _deleteOnDispose; private readonly bool _deleteOnDispose;
@ -24,6 +25,7 @@ public class TemporaryFileManager : IDisposable
{ {
if (!_deleteOnDispose) return; if (!_deleteOnDispose) return;
for (var retries = 0; retries < 10; retries++) for (var retries = 0; retries < 10; retries++)
{
try try
{ {
if (!_basePath.DirectoryExists()) if (!_basePath.DirectoryExists())
@ -31,10 +33,31 @@ public class TemporaryFileManager : IDisposable
_basePath.DeleteDirectory(); _basePath.DeleteDirectory();
return; return;
} }
catch (IOException ex) catch (IOException)
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
} }
}
}
public async ValueTask DisposeAsync()
{
if (!_deleteOnDispose) return;
for (var retries = 0; retries < 10; retries++)
{
try
{
if (!_basePath.DirectoryExists())
return;
_basePath.DeleteDirectory();
return;
}
catch (IOException)
{
await Task.Delay(1000);
}
}
} }
public TemporaryPath CreateFile(Extension? ext = default, bool deleteOnDispose = true) public TemporaryPath CreateFile(Extension? ext = default, bool deleteOnDispose = true)
@ -51,4 +74,5 @@ public class TemporaryFileManager : IDisposable
path.CreateDirectory(); path.CreateDirectory();
return new TemporaryPath(path); return new TemporaryPath(path);
} }
} }

View File

@ -27,8 +27,9 @@ public struct TemporaryPath : IDisposable, IAsyncDisposable
return tp.Path; return tp.Path;
} }
public async ValueTask DisposeAsync() public ValueTask DisposeAsync()
{ {
Path.Delete(); Path.Delete();
return ValueTask.CompletedTask;
} }
} }