From e67a127a02fb1020f137dbeb9a206eb75da59aab Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 28 Jul 2024 20:16:51 -0700 Subject: [PATCH] Fixed: Allow leading/trailing spaces on non-Windows (cherry picked from commit 9127a91dfc460f442498a00faed98737047098cd) --- .../PathExtensionFixture.cs | 11 ++++++++++- .../Extensions/PathExtensions.cs | 18 +++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/NzbDrone.Common.Test/PathExtensionFixture.cs b/src/NzbDrone.Common.Test/PathExtensionFixture.cs index 344c6faf3..3fe72c058 100644 --- a/src/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/src/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -337,8 +337,17 @@ namespace NzbDrone.Common.Test [TestCase(@" C:\Test\TV\")] [TestCase(@" C:\Test\TV")] - public void IsPathValid_should_be_false(string path) + public void IsPathValid_should_be_false_on_windows(string path) { + WindowsOnly(); + path.IsPathValid(PathValidationType.CurrentOs).Should().BeFalse(); + } + + [TestCase(@"")] + [TestCase(@"relative/path")] + public void IsPathValid_should_be_false_on_unix(string path) + { + PosixOnly(); path.AsOsAgnostic().IsPathValid(PathValidationType.CurrentOs).Should().BeFalse(); } } diff --git a/src/NzbDrone.Common/Extensions/PathExtensions.cs b/src/NzbDrone.Common/Extensions/PathExtensions.cs index 4816c9f27..0a98cef4c 100644 --- a/src/NzbDrone.Common/Extensions/PathExtensions.cs +++ b/src/NzbDrone.Common/Extensions/PathExtensions.cs @@ -155,16 +155,20 @@ namespace NzbDrone.Common.Extensions return false; } - var directoryInfo = new DirectoryInfo(path); - - while (directoryInfo != null) + // Only check for leading or trailing spaces for path when running on Windows. + if (OsInfo.IsWindows) { - if (directoryInfo.Name.Trim() != directoryInfo.Name) + var directoryInfo = new DirectoryInfo(path); + + while (directoryInfo != null) { - return false; - } + if (directoryInfo.Name.Trim() != directoryInfo.Name) + { + return false; + } - directoryInfo = directoryInfo.Parent; + directoryInfo = directoryInfo.Parent; + } } if (validationType == PathValidationType.AnyOs)