mirror of https://github.com/Sonarr/Sonarr
Fixed recursion issue when emptying recycle bin
This commit is contained in:
parent
4d1a4d4241
commit
5251db7224
|
@ -212,6 +212,38 @@ namespace NzbDrone.Common.Test.DiskTests
|
||||||
Subject.GetParentFolder(path).Should().Be(parent);
|
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)
|
private void DoHardLinkRename(FileShare fileShare)
|
||||||
{
|
{
|
||||||
var sourceDir = GetTempFilePath();
|
var sourceDir = GetTempFilePath();
|
||||||
|
|
|
@ -145,7 +145,7 @@ namespace NzbDrone.Common.Disk
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath();
|
||||||
|
|
||||||
return Directory.EnumerateDirectories(path).Empty();
|
return Directory.EnumerateFileSystemEntries(path).Empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string[] GetDirectories(string path)
|
public string[] GetDirectories(string path)
|
||||||
|
@ -489,14 +489,21 @@ namespace NzbDrone.Common.Disk
|
||||||
|
|
||||||
public void RemoveEmptySubfolders(string path)
|
public void RemoveEmptySubfolders(string path)
|
||||||
{
|
{
|
||||||
var subfolders = Directory.GetDirectories(path, "*", SearchOption.AllDirectories);
|
// Depth first search for empty subdirectories
|
||||||
var files = GetFiles(path, SearchOption.AllDirectories);
|
foreach (var subdir in Directory.EnumerateDirectories(path))
|
||||||
|
{
|
||||||
|
RemoveEmptySubfolders(subdir);
|
||||||
|
|
||||||
foreach (var subfolder in subfolders)
|
if (Directory.EnumerateFileSystemEntries(subdir).Empty())
|
||||||
{
|
{
|
||||||
if (files.None(f => subfolder.IsParentPath(f)))
|
try
|
||||||
{
|
{
|
||||||
DeleteFolder(subfolder, false);
|
Directory.Delete(subdir, false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.Warn(ex, "Failed to remove empty directory {0}", subdir);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -202,14 +202,12 @@ namespace NzbDrone.Core.MediaFiles
|
||||||
{
|
{
|
||||||
if (_configService.DeleteEmptyFolders)
|
if (_configService.DeleteEmptyFolders)
|
||||||
{
|
{
|
||||||
if (_diskProvider.GetFiles(path, SearchOption.AllDirectories).Empty())
|
_diskProvider.RemoveEmptySubfolders(path);
|
||||||
|
|
||||||
|
if (_diskProvider.FolderEmpty(path))
|
||||||
{
|
{
|
||||||
_diskProvider.DeleteFolder(path, true);
|
_diskProvider.DeleteFolder(path, true);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
_diskProvider.RemoveEmptySubfolders(path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue