1
0
mirror of https://github.com/oliverbooth/fdup.git synced 2024-11-10 05:15:43 +00:00

Compare commits

..

No commits in common. "921c1b19c1b93fb53b0ebb0087a5d89909431991" and "801dfe09cb005256b157348dd470142edb59804e" have entirely different histories.

View File

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