1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Fixed recursion issue when emptying recycle bin

This commit is contained in:
Taloth Saldono 2020-05-16 19:08:55 +02:00 committed by Qstick
parent 83679214b3
commit e8e852100c
3 changed files with 49 additions and 12 deletions

View File

@ -213,6 +213,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();

View File

@ -146,7 +146,7 @@ public bool FolderEmpty(string path)
{
Ensure.That(path, () => path).IsValidPath();
return Directory.EnumerateDirectories(path).Empty();
return Directory.EnumerateFileSystemEntries(path).Empty();
}
public string[] GetDirectories(string path)
@ -526,14 +526,21 @@ public List<FileInfo> GetFileInfos(string path)
public void RemoveEmptySubfolders(string path)
{
var subfolders = GetDirectories(path);
var files = GetFiles(path, SearchOption.AllDirectories);
foreach (var subfolder in subfolders)
// 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);
}
}
}
}

View File

@ -206,14 +206,12 @@ private void RemoveEmptyMovieFolder(string path)
{
if (_configService.DeleteEmptyFolders)
{
if (_diskProvider.GetFiles(path, SearchOption.AllDirectories).Empty())
_diskProvider.RemoveEmptySubfolders(path);
if (_diskProvider.FolderEmpty(path))
{
_diskProvider.DeleteFolder(path, true);
}
else
{
_diskProvider.RemoveEmptySubfolders(path);
}
}
}