mirror of
https://github.com/oliverbooth/fdup.git
synced 2024-11-10 00:05:42 +00:00
Compare commits
3 Commits
9f7e605d38
...
d2ca59e174
Author | SHA1 | Date | |
---|---|---|---|
d2ca59e174 | |||
6bcb4abc99 | |||
a6a8f85ce9 |
@ -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>
|
||||||
|
|
||||||
|
@ -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)
|
||||||
{
|
{
|
||||||
@ -66,35 +93,54 @@ 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}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.WhenAll(tasks);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user