mirror of
https://github.com/oliverbooth/fdup.git
synced 2024-12-05 02:08:48 +00:00
fix: prevent application break on I/O exception
This commit is contained in:
parent
a6a8f85ce9
commit
6bcb4abc99
@ -1,5 +1,6 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Security;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Spectre.Console;
|
||||
@ -91,11 +92,20 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
|
||||
AnsiConsole.MarkupLineInterpolated($"Searching [cyan]{relativePath}[/]");
|
||||
|
||||
if (settings.Recursive)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (DirectoryInfo childDirectory in currentDirectory.EnumerateDirectories())
|
||||
directoryStack.Push(childDirectory);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
foreach (FileInfo file in currentDirectory.EnumerateFiles())
|
||||
{
|
||||
string relativeFilePath = Path.GetRelativePath(inputDirectory.FullName, file.FullName);
|
||||
@ -103,11 +113,18 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
|
||||
tasks.Add(Task.Run(() => ProcessFile(file)));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessFile(FileInfo file)
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[64];
|
||||
try
|
||||
{
|
||||
using FileStream stream = file.OpenRead();
|
||||
using BufferedStream bufferedStream = new BufferedStream(stream, 1048576 /* 1MB */);
|
||||
SHA512.HashData(bufferedStream, buffer);
|
||||
@ -120,6 +137,11 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
|
||||
lock (cache)
|
||||
cache.Add(file);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static string ByteSpanToString(ReadOnlySpan<byte> buffer)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user