1
0
mirror of https://github.com/oliverbooth/fdup.git synced 2024-11-10 03:45:42 +00:00

Compare commits

..

No commits in common. "d2ca59e174ca9cd3392c46d0249a3023089a1188" and "9f7e605d386007082e1597a9a7157dfb95e9f217" have entirely different histories.

2 changed files with 20 additions and 66 deletions

View File

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

View File

@ -1,6 +1,5 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using Spectre.Console;
@ -24,9 +23,7 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
AnsiConsole.MarkupLineInterpolated($"Searching [cyan]{inputDirectory.FullName}[/]");
AnsiConsole.MarkupLine($"Recursive mode is {(settings.Recursive ? "[green]ON" : "[red]OFF")}[/]");
await AnsiConsole.Status()
.StartAsync("Waiting to hash files...", DoHashWaitAsync)
.ConfigureAwait(false);
await SearchAsync(inputDirectory, settings);
AnsiConsole.WriteLine();
@ -54,35 +51,11 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
AnsiConsole.MarkupLineInterpolated($"[yellow]Found [cyan]{duplicates}[/] duplicates![/]");
return 0;
async Task DoHashWaitAsync(StatusContext ctx)
{
await WaitForHashCompletionAsync(settings, inputDirectory, ctx);
}
}
private async Task WaitForHashCompletionAsync(ListSettings settings,
DirectoryInfo inputDirectory,
StatusContext ctx)
private async Task SearchAsync(DirectoryInfo inputDirectory, ListSettings settings)
{
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]);
while (directoryStack.Count > 0)
{
@ -92,20 +65,11 @@ 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);
@ -113,18 +77,13 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
tasks.Add(Task.Run(() => ProcessFile(file)));
}
}
catch (Exception ex)
{
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
}
}
await Task.WhenAll(tasks);
}
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);
@ -137,11 +96,6 @@ 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)
{