1
0
mirror of https://github.com/oliverbooth/fdup.git synced 2024-12-05 02:28:47 +00:00

fix: prevent application break on I/O exception

This commit is contained in:
Oliver Booth 2024-04-16 21:00:02 +01:00
parent a6a8f85ce9
commit 6bcb4abc99
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5

View File

@ -1,5 +1,6 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Diagnostics; using System.Diagnostics;
using System.Security;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using Spectre.Console; using Spectre.Console;
@ -92,15 +93,29 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
if (settings.Recursive) if (settings.Recursive)
{ {
foreach (DirectoryInfo childDirectory in currentDirectory.EnumerateDirectories()) try
directoryStack.Push(childDirectory); {
foreach (DirectoryInfo childDirectory in currentDirectory.EnumerateDirectories())
directoryStack.Push(childDirectory);
}
catch (Exception ex)
{
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
}
} }
foreach (FileInfo file in currentDirectory.EnumerateFiles()) try
{ {
string relativeFilePath = Path.GetRelativePath(inputDirectory.FullName, file.FullName); foreach (FileInfo file in currentDirectory.EnumerateFiles())
AnsiConsole.MarkupLineInterpolated($"Checking hash for [cyan]{relativeFilePath}[/]"); {
tasks.Add(Task.Run(() => ProcessFile(file))); string relativeFilePath = Path.GetRelativePath(inputDirectory.FullName, file.FullName);
AnsiConsole.MarkupLineInterpolated($"Checking hash for [cyan]{relativeFilePath}[/]");
tasks.Add(Task.Run(() => ProcessFile(file)));
}
}
catch (Exception ex)
{
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
} }
} }
} }
@ -108,17 +123,24 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
private void ProcessFile(FileInfo file) private void ProcessFile(FileInfo file)
{ {
Span<byte> buffer = stackalloc byte[64]; Span<byte> buffer = stackalloc byte[64];
using FileStream stream = file.OpenRead(); try
using BufferedStream bufferedStream = new BufferedStream(stream, 1048576 /* 1MB */); {
SHA512.HashData(bufferedStream, buffer); using FileStream stream = file.OpenRead();
string hash = ByteSpanToString(buffer); using BufferedStream bufferedStream = new BufferedStream(stream, 1048576 /* 1MB */);
Trace.WriteLine($"{file.FullName}: {hash}"); SHA512.HashData(bufferedStream, buffer);
string hash = ByteSpanToString(buffer);
Trace.WriteLine($"{file.FullName}: {hash}");
if (!_fileHashMap.TryGetValue(hash, out List<FileInfo>? cache)) if (!_fileHashMap.TryGetValue(hash, out List<FileInfo>? cache))
_fileHashMap[hash] = cache = new List<FileInfo>(); _fileHashMap[hash] = cache = new List<FileInfo>();
lock (cache) lock (cache)
cache.Add(file); cache.Add(file);
}
catch (Exception ex)
{
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
}
} }
private static string ByteSpanToString(ReadOnlySpan<byte> buffer) private static string ByteSpanToString(ReadOnlySpan<byte> buffer)