From 96830f975e82593d98087c6e60227034ff6d6d7e Mon Sep 17 00:00:00 2001 From: Bogdan Date: Tue, 8 Oct 2024 01:29:30 +0300 Subject: [PATCH] Cleaning paths for top level root folders (cherry picked from commit a00121695715feb2cf8f04da246dc18262ab3237) --- .../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 41405dc05..010bc3a02 100644 --- a/src/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/src/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -393,6 +393,27 @@ namespace NzbDrone.Common.Test 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); + } + [TestCase(@"C:\Test\", @"C:\Test\Series Title", "Series Title")] [TestCase(@"C:\Test\", @"C:\Test\Collection\Series Title", @"Collection\Series Title")] [TestCase(@"C:\Test\mydir\", @"C:\Test\mydir\Collection\Series Title", @"Collection\Series Title")] diff --git a/src/NzbDrone.Common/Disk/OsPath.cs b/src/NzbDrone.Common/Disk/OsPath.cs index 0506df768..30661d747 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 78f0aaf1a..30a467f21 100644 --- a/src/NzbDrone.Common/Extensions/PathExtensions.cs +++ b/src/NzbDrone.Common/Extensions/PathExtensions.cs @@ -25,8 +25,6 @@ namespace NzbDrone.Common.Extensions private static readonly string UPDATE_CLIENT_FOLDER_NAME = "Prowlarr.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(@"(?