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