1
0
mirror of https://github.com/oliverbooth/fdup.git synced 2024-11-09 23:45:41 +00:00

Compare commits

...

3 Commits

2 changed files with 66 additions and 20 deletions

View File

@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AssemblyName>fdup</AssemblyName> <AssemblyName>fdup</AssemblyName>
<VersionPrefix>1.0.1</VersionPrefix> <VersionPrefix>1.1.0</VersionPrefix>
<Authors>Oliver Booth</Authors> <Authors>Oliver Booth</Authors>
</PropertyGroup> </PropertyGroup>

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;
@ -23,7 +24,9 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
AnsiConsole.MarkupLineInterpolated($"Searching [cyan]{inputDirectory.FullName}[/]"); AnsiConsole.MarkupLineInterpolated($"Searching [cyan]{inputDirectory.FullName}[/]");
AnsiConsole.MarkupLine($"Recursive mode is {(settings.Recursive ? "[green]ON" : "[red]OFF")}[/]"); AnsiConsole.MarkupLine($"Recursive mode is {(settings.Recursive ? "[green]ON" : "[red]OFF")}[/]");
await SearchAsync(inputDirectory, settings); await AnsiConsole.Status()
.StartAsync("Waiting to hash files...", DoHashWaitAsync)
.ConfigureAwait(false);
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
@ -51,11 +54,35 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
AnsiConsole.MarkupLineInterpolated($"[yellow]Found [cyan]{duplicates}[/] duplicates![/]"); AnsiConsole.MarkupLineInterpolated($"[yellow]Found [cyan]{duplicates}[/] duplicates![/]");
return 0; return 0;
async Task DoHashWaitAsync(StatusContext ctx)
{
await WaitForHashCompletionAsync(settings, inputDirectory, ctx);
}
} }
private async Task SearchAsync(DirectoryInfo inputDirectory, ListSettings settings) private async Task WaitForHashCompletionAsync(ListSettings settings,
DirectoryInfo inputDirectory,
StatusContext ctx)
{ {
var tasks = new List<Task>(); var tasks = new List<Task>();
SearchDuplicates(inputDirectory, settings, tasks);
await Task.Run(() =>
{
int incompleteTasks;
do
{
incompleteTasks = tasks.Count(t => !t.IsCompleted);
ctx.Status($"Waiting to hash {incompleteTasks} {(incompleteTasks == 1 ? "file" : "files")}...");
ctx.Refresh();
} while (tasks.Count > 0 && incompleteTasks > 0);
ctx.Status("Hash complete");
}).ConfigureAwait(false);
}
private void SearchDuplicates(DirectoryInfo inputDirectory, ListSettings settings, ICollection<Task> tasks)
{
var directoryStack = new Stack<DirectoryInfo>([inputDirectory]); var directoryStack = new Stack<DirectoryInfo>([inputDirectory]);
while (directoryStack.Count > 0) while (directoryStack.Count > 0)
{ {
@ -65,11 +92,20 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
AnsiConsole.MarkupLineInterpolated($"Searching [cyan]{relativePath}[/]"); AnsiConsole.MarkupLineInterpolated($"Searching [cyan]{relativePath}[/]");
if (settings.Recursive) if (settings.Recursive)
{
try
{ {
foreach (DirectoryInfo childDirectory in currentDirectory.EnumerateDirectories()) foreach (DirectoryInfo childDirectory in currentDirectory.EnumerateDirectories())
directoryStack.Push(childDirectory); directoryStack.Push(childDirectory);
} }
catch (Exception ex)
{
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
}
}
try
{
foreach (FileInfo file in currentDirectory.EnumerateFiles()) foreach (FileInfo file in currentDirectory.EnumerateFiles())
{ {
string relativeFilePath = Path.GetRelativePath(inputDirectory.FullName, file.FullName); string relativeFilePath = Path.GetRelativePath(inputDirectory.FullName, file.FullName);
@ -77,13 +113,18 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
tasks.Add(Task.Run(() => ProcessFile(file))); tasks.Add(Task.Run(() => ProcessFile(file)));
} }
} }
catch (Exception ex)
await Task.WhenAll(tasks); {
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
}
}
} }
private void ProcessFile(FileInfo file) private void ProcessFile(FileInfo file)
{ {
Span<byte> buffer = stackalloc byte[64]; Span<byte> buffer = stackalloc byte[64];
try
{
using FileStream stream = file.OpenRead(); using FileStream stream = file.OpenRead();
using BufferedStream bufferedStream = new BufferedStream(stream, 1048576 /* 1MB */); using BufferedStream bufferedStream = new BufferedStream(stream, 1048576 /* 1MB */);
SHA512.HashData(bufferedStream, buffer); SHA512.HashData(bufferedStream, buffer);
@ -96,6 +137,11 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
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)
{ {