From c13195046d6122aed0a70cacdefdfbc7193d881e Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Tue, 20 Aug 2013 12:28:46 -0700 Subject: [PATCH] case insensitive match for unmapped folders. --- .../DiskProviderTests/DiskProviderFixture.cs | 22 ----------------- NzbDrone.Common.Test/PathExtensionFixture.cs | 24 +++++++++++++++++++ NzbDrone.Common/DiskProvider.cs | 17 ++++--------- NzbDrone.Common/PathExtensions.cs | 11 ++++++++- .../MediaFiles/EpisodeFileMovingService.cs | 2 +- .../RootFolders/RootFolderService.cs | 8 +++---- 6 files changed, 44 insertions(+), 40 deletions(-) diff --git a/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixture.cs b/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixture.cs index 435e953ce..1bcb330f1 100644 --- a/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixture.cs +++ b/NzbDrone.Common.Test/DiskProviderTests/DiskProviderFixture.cs @@ -127,29 +127,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests } - [TestCase(@"C:\", @"C:\")] - [TestCase(@"C:\\", @"C:\")] - [TestCase(@"c:\", @"C:\")] - [TestCase(@"c:\Test", @"C:\Test\\")] - [TestCase(@"c:\\\\\Test", @"C:\Test\\")] - [TestCase(@"c:\Test\\\\", @"C:\Test\\")] - [TestCase(@"c:\Test", @"C:\Test\\")] - [TestCase(@"\\Server\pool", @"\\Server\pool")] - [TestCase(@"\\Server\pool\", @"\\Server\pool")] - [TestCase(@"\\Server\pool", @"\\Server\pool\")] - [TestCase(@"\\Server\pool\", @"\\Server\pool\")] - [TestCase(@"\\smallcheese\DRIVE_G\TV-C\Simspsons", @"\\smallcheese\DRIVE_G\TV-C\Simspsons")] - public void paths_should_be_equal(string first, string second) - { - DiskProvider.PathEquals(first.AsOsAgnostic(), second.AsOsAgnostic()).Should().BeTrue(); - } - [TestCase(@"C:\Test", @"C:\Test2\")] - [TestCase(@"C:\Test\Test", @"C:\TestTest\")] - public void paths_should_not_be_equal(string first, string second) - { - DiskProvider.PathEquals(first.AsOsAgnostic(), second.AsOsAgnostic()).Should().BeFalse(); - } [Test] public void empty_folder_should_return_folder_modified_date() diff --git a/NzbDrone.Common.Test/PathExtensionFixture.cs b/NzbDrone.Common.Test/PathExtensionFixture.cs index f8ca1b8ae..db4645dfa 100644 --- a/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -56,6 +56,30 @@ namespace NzbDrone.Common.Test result.Should().Be(clean); } + [TestCase(@"C:\", @"C:\")] + [TestCase(@"C:\\", @"C:\")] + [TestCase(@"c:\", @"C:\")] + [TestCase(@"c:\Test", @"C:\Test\\")] + [TestCase(@"c:\\\\\Test", @"C:\Test\\")] + [TestCase(@"c:\Test\\\\", @"C:\Test\\")] + [TestCase(@"c:\Test", @"C:\Test\\")] + [TestCase(@"\\Server\pool", @"\\Server\pool")] + [TestCase(@"\\Server\pool\", @"\\Server\pool")] + [TestCase(@"\\Server\pool", @"\\Server\pool\")] + [TestCase(@"\\Server\pool\", @"\\Server\pool\")] + [TestCase(@"\\smallcheese\DRIVE_G\TV-C\Simspsons", @"\\smallcheese\DRIVE_G\TV-C\Simspsons")] + public void paths_should_be_equal(string first, string second) + { + first.AsOsAgnostic().PathEquals(second.AsOsAgnostic()).Should().BeTrue(); + } + + [TestCase(@"C:\Test", @"C:\Test2\")] + [TestCase(@"C:\Test\Test", @"C:\TestTest\")] + public void paths_should_not_be_equal(string first, string second) + { + first.AsOsAgnostic().PathEquals(second.AsOsAgnostic()).Should().BeFalse(); + } + [Test] public void normalize_path_exception_empty() { diff --git a/NzbDrone.Common/DiskProvider.cs b/NzbDrone.Common/DiskProvider.cs index f1b780ff4..9ffdb8a77 100644 --- a/NzbDrone.Common/DiskProvider.cs +++ b/NzbDrone.Common/DiskProvider.cs @@ -41,12 +41,12 @@ namespace NzbDrone.Common string GetPathRoot(string path); void SetPermissions(string filename, WellKnownSidType accountSid, FileSystemRights rights, AccessControlType controlType); bool IsParent(string parentPath, string childPath); - FileAttributes GetFileAttributes(string path); + FileAttributes GetFileAttributes(string path); } public class DiskProvider : IDiskProvider { - enum TransferAction + enum TransferAction { Copy, Move @@ -260,7 +260,7 @@ namespace NzbDrone.Common Ensure.That(() => source).IsValidPath(); Ensure.That(() => destination).IsValidPath(); - if (PathEquals(source, destination)) + if (source.PathEquals(destination)) { Logger.Warn("Source and destination can't be the same {0}", source); return; @@ -303,7 +303,7 @@ namespace NzbDrone.Common throw new DirectoryNotFoundException(path); } - return driveInfo.AvailableFreeSpace; + return driveInfo.AvailableFreeSpace; } var root = GetPathRoot(path); @@ -312,7 +312,7 @@ namespace NzbDrone.Common throw new DirectoryNotFoundException(root); return DriveFreeSpaceEx(root); - } + } private static long DriveFreeSpaceEx(string folderName) { @@ -352,13 +352,6 @@ namespace NzbDrone.Common File.WriteAllText(filename, contents); } - public static bool PathEquals(string firstPath, string secondPath) - { - Ensure.That(() => firstPath).IsValidPath(); - Ensure.That(() => secondPath).IsValidPath(); - - return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath(), StringComparison.InvariantCultureIgnoreCase); - } public void FileSetLastWriteTimeUtc(string path, DateTime dateTime) { diff --git a/NzbDrone.Common/PathExtensions.cs b/NzbDrone.Common/PathExtensions.cs index 7c2eea7a5..4e7d2251c 100644 --- a/NzbDrone.Common/PathExtensions.cs +++ b/NzbDrone.Common/PathExtensions.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using NzbDrone.Common.EnsureThat; using NzbDrone.Common.EnvironmentInfo; @@ -35,6 +36,14 @@ namespace NzbDrone.Common } + public static bool PathEquals(this string firstPath, string secondPath) + { + Ensure.That(() => firstPath).IsValidPath(); + Ensure.That(() => secondPath).IsValidPath(); + + return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath(), StringComparison.InvariantCultureIgnoreCase); + } + public static bool ContainsInvalidPathChars(this string text) { return text.IndexOfAny(Path.GetInvalidPathChars()) >= 0; diff --git a/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs b/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs index 8eba9e0d4..e46bf3b26 100644 --- a/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs +++ b/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs @@ -65,7 +65,7 @@ namespace NzbDrone.Core.MediaFiles throw new FileNotFoundException("Episode file path does not exist", episodeFile.Path); } - if (DiskProvider.PathEquals(episodeFile.Path, destinationFilename)) + if (episodeFile.Path.PathEquals(destinationFilename)) { throw new SameFilenameException("File not moved, source and destination are the same", episodeFile.Path); } diff --git a/NzbDrone.Core/RootFolders/RootFolderService.cs b/NzbDrone.Core/RootFolders/RootFolderService.cs index 442f4b32e..a27fa01a7 100644 --- a/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -73,11 +73,11 @@ namespace NzbDrone.Core.RootFolders if (!_diskProvider.FolderExists(rootFolder.Path)) throw new DirectoryNotFoundException("Can't add root directory that doesn't exist."); - if (all.Exists(r => DiskProvider.PathEquals(r.Path, rootFolder.Path))) + if (all.Exists(r => r.Path.PathEquals(rootFolder.Path))) throw new InvalidOperationException("Recent directory already exists."); if (!String.IsNullOrWhiteSpace(_configService.DownloadedEpisodesFolder) && - DiskProvider.PathEquals(_configService.DownloadedEpisodesFolder, rootFolder.Path)) + _configService.DownloadedEpisodesFolder.PathEquals(rootFolder.Path)) throw new InvalidOperationException("Drone Factory folder cannot be used."); _rootFolderRepository.Insert(rootFolder); @@ -109,10 +109,10 @@ namespace NzbDrone.Core.RootFolders foreach (string seriesFolder in _diskProvider.GetDirectories(path)) { - if (!series.Any(s => s.Path == seriesFolder)) + if (!series.Any(s => s.Path.PathEquals(seriesFolder))) { var di = new DirectoryInfo(seriesFolder.Normalize()); - results.Add(new UnmappedFolder{ Name = di.Name, Path = di.FullName }); + results.Add(new UnmappedFolder { Name = di.Name, Path = di.FullName }); } }