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

Compare commits

..

3 Commits

View File

@ -8,6 +8,7 @@ namespace FindDuplicates;
internal sealed class ListCommand : AsyncCommand<ListSettings>
{
private readonly ConcurrentDictionary<long, ConcurrentBag<FileInfo>> _fileSizeMap = new();
private readonly ConcurrentDictionary<string, ConcurrentBag<FileInfo>> _fileHashMap = new();
public override async Task<int> ExecuteAsync(CommandContext context, ListSettings settings)
@ -15,7 +16,7 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
var inputDirectory = new DirectoryInfo(settings.InputPath);
if (!inputDirectory.Exists)
{
AnsiConsole.MarkupLine($"[red]{inputDirectory} does not exist![/]");
AnsiConsole.MarkupLineInterpolated($"[red]{inputDirectory} does not exist![/]");
return -1;
}
@ -65,7 +66,7 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
StatusContext ctx)
{
var tasks = new List<Task>();
SearchDuplicates(inputDirectory, settings, tasks);
SearchDuplicates(ctx, inputDirectory, settings, tasks);
await Task.Run(() =>
{
int incompleteTasks;
@ -80,15 +81,14 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
}).ConfigureAwait(false);
}
private void SearchDuplicates(DirectoryInfo inputDirectory, ListSettings settings, ICollection<Task> tasks)
private void SearchDuplicates(StatusContext ctx, DirectoryInfo inputDirectory, ListSettings settings, ICollection<Task> tasks)
{
var directoryStack = new Stack<DirectoryInfo>([inputDirectory]);
while (directoryStack.Count > 0)
{
DirectoryInfo currentDirectory = directoryStack.Pop();
string relativePath = Path.GetRelativePath(inputDirectory.FullName, currentDirectory.FullName);
if (relativePath != ".")
AnsiConsole.MarkupLineInterpolated($"Searching [cyan]{relativePath}[/]");
ctx.Status(currentDirectory.FullName.EscapeMarkup());
AddChildDirectories(settings, currentDirectory, directoryStack);
@ -96,9 +96,15 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
{
foreach (FileInfo file in currentDirectory.EnumerateFiles())
{
string relativeFilePath = Path.GetRelativePath(inputDirectory.FullName, file.FullName);
AnsiConsole.MarkupLineInterpolated($"Checking hash for [cyan]{relativeFilePath}[/]");
tasks.Add(Task.Run(() => ProcessFile(file, settings)));
try
{
ConcurrentBag<FileInfo> cache = _fileSizeMap.GetOrAdd(file.Length, _ => []);
cache.Add(file);
}
catch (Exception ex)
{
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
}
}
}
catch (Exception ex)
@ -106,6 +112,30 @@ internal sealed class ListCommand : AsyncCommand<ListSettings>
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
}
}
foreach ((_, ConcurrentBag<FileInfo> files) in _fileSizeMap)
{
if (files.Count < 1)
continue;
tasks.Add(Task.Run(() =>
{
foreach (FileInfo file in files)
{
try
{
AnsiConsole.MarkupLineInterpolated($"Checking hash for [cyan]{file.Name}[/]");
ProcessFile(file, settings);
}
catch (Exception ex)
{
AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}");
}
}
}));
}
_fileSizeMap.Clear();
}
private void ProcessFile(FileInfo file, ListSettings settings)