From c9836f997cc492c15e035fe74b90d4bf2b321273 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Fri, 4 Oct 2024 12:40:37 +0300 Subject: [PATCH] Fixed: Clean paths for top level root folders --- .../PathExtensionFixture.cs | 21 +++++++++++++++++++ src/NzbDrone.Common/Disk/OsPath.cs | 14 +++++++++++-- .../Extensions/PathExtensions.cs | 8 ++----- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/NzbDrone.Common.Test/PathExtensionFixture.cs b/src/NzbDrone.Common.Test/PathExtensionFixture.cs index f0a42b0cd..13f28b681 100644 --- a/src/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/src/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -392,5 +392,26 @@ namespace NzbDrone.Common.Test PosixOnly(); path.AsOsAgnostic().IsPathValid(PathValidationType.CurrentOs).Should().BeFalse(); } + + [TestCase(@"C:\", @"C:\")] + [TestCase(@"C:\\", @"C:\")] + [TestCase(@"C:\Test", @"C:\Test")] + [TestCase(@"C:\Test\", @"C:\Test")] + [TestCase(@"\\server\share", @"\\server\share")] + [TestCase(@"\\server\share\", @"\\server\share")] + public void windows_path_should_return_clean_path(string path, string cleanPath) + { + path.GetCleanPath().Should().Be(cleanPath); + } + + [TestCase("/", "/")] + [TestCase("//", "/")] + [TestCase("/test", "/test")] + [TestCase("/test/", "/test")] + [TestCase("/test//", "/test")] + public void unix_path_should_return_clean_path(string path, string cleanPath) + { + path.GetCleanPath().Should().Be(cleanPath); + } } } diff --git a/src/NzbDrone.Common/Disk/OsPath.cs b/src/NzbDrone.Common/Disk/OsPath.cs index 45e520761..42fdaf567 100644 --- a/src/NzbDrone.Common/Disk/OsPath.cs +++ b/src/NzbDrone.Common/Disk/OsPath.cs @@ -104,9 +104,19 @@ namespace NzbDrone.Common.Disk switch (kind) { case OsPathKind.Windows when !path.EndsWith(":\\"): - return path.TrimEnd('\\'); + while (!path.EndsWith(":\\") && path.EndsWith('\\')) + { + path = path[..^1]; + } + + return path; case OsPathKind.Unix when path != "/": - return path.TrimEnd('/'); + while (path != "/" && path.EndsWith('/')) + { + path = path[..^1]; + } + + return path; } return path; diff --git a/src/NzbDrone.Common/Extensions/PathExtensions.cs b/src/NzbDrone.Common/Extensions/PathExtensions.cs index 2424323fc..865e25138 100644 --- a/src/NzbDrone.Common/Extensions/PathExtensions.cs +++ b/src/NzbDrone.Common/Extensions/PathExtensions.cs @@ -26,8 +26,6 @@ namespace NzbDrone.Common.Extensions private static readonly string UPDATE_CLIENT_FOLDER_NAME = "Radarr.Update" + Path.DirectorySeparatorChar; private static readonly string UPDATE_LOG_FOLDER_NAME = "UpdateLogs" + Path.DirectorySeparatorChar; - private static readonly Regex PARENT_PATH_END_SLASH_REGEX = new Regex(@"(?