mirror of
https://github.com/lidarr/Lidarr
synced 2025-01-03 05:25:10 +00:00
Fixed recursion issue when emptying recycle bin
Co-Authored-By: Taloth <Taloth@users.noreply.github.com>
This commit is contained in:
parent
67822e6214
commit
77328559e9
2 changed files with 45 additions and 7 deletions
|
@ -224,6 +224,38 @@ public void GetParentFolder_should_remove_trailing_slash_before_getting_parent_f
|
|||
Subject.GetParentFolder(path).Should().Be(parent);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveEmptySubfolders_should_remove_nested_empty_folder()
|
||||
{
|
||||
var mainDir = GetTempFilePath();
|
||||
var subDir1 = Path.Combine(mainDir, "depth1");
|
||||
var subDir2 = Path.Combine(subDir1, "depth2");
|
||||
Directory.CreateDirectory(subDir2);
|
||||
|
||||
Subject.RemoveEmptySubfolders(mainDir);
|
||||
|
||||
Directory.Exists(mainDir).Should().Be(true);
|
||||
Directory.Exists(subDir1).Should().Be(false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveEmptySubfolders_should_not_remove_nested_nonempty_folder()
|
||||
{
|
||||
var mainDir = GetTempFilePath();
|
||||
var subDir1 = Path.Combine(mainDir, "depth1");
|
||||
var subDir2 = Path.Combine(subDir1, "depth2");
|
||||
var file = Path.Combine(subDir1, "file1.txt");
|
||||
Directory.CreateDirectory(subDir2);
|
||||
File.WriteAllText(file, "I should not be deleted");
|
||||
|
||||
Subject.RemoveEmptySubfolders(mainDir);
|
||||
|
||||
Directory.Exists(mainDir).Should().Be(true);
|
||||
Directory.Exists(subDir1).Should().Be(true);
|
||||
Directory.Exists(subDir2).Should().Be(false);
|
||||
File.Exists(file).Should().Be(true);
|
||||
}
|
||||
|
||||
private void DoHardLinkRename(FileShare fileShare)
|
||||
{
|
||||
var sourceDir = GetTempFilePath();
|
||||
|
|
|
@ -517,15 +517,21 @@ public IFileInfo GetFileInfo(string path)
|
|||
|
||||
public void RemoveEmptySubfolders(string path)
|
||||
{
|
||||
var subfolders = GetDirectories(path, SearchOption.AllDirectories);
|
||||
var files = GetFiles(path, SearchOption.AllDirectories);
|
||||
|
||||
// By sorting by length descending we ensure we always delete children before parents
|
||||
foreach (var subfolder in subfolders.OrderByDescending(x => x.Length))
|
||||
// Depth first search for empty subdirectories
|
||||
foreach (var subdir in Directory.EnumerateDirectories(path))
|
||||
{
|
||||
if (files.None(f => subfolder.IsParentPath(f)))
|
||||
RemoveEmptySubfolders(subdir);
|
||||
|
||||
if (Directory.EnumerateFileSystemEntries(subdir).Empty())
|
||||
{
|
||||
DeleteFolder(subfolder, false);
|
||||
try
|
||||
{
|
||||
Directory.Delete(subdir, false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warn(ex, "Failed to remove empty directory {0}", subdir);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue