From ff12c36204b23c43feb971a1e0c5a6ab88a9f22a Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 13 Jul 2024 21:24:29 -0700 Subject: [PATCH] fixup! Fixed: Updating series path from different OS paths --- .../PathExtensionFixture.cs | 3 +- src/NzbDrone.Common/Disk/OsPath.cs | 42 +++++++++++++++---- .../Extensions/PathExtensions.cs | 24 +++-------- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/NzbDrone.Common.Test/PathExtensionFixture.cs b/src/NzbDrone.Common.Test/PathExtensionFixture.cs index 85de3dc60..0eddbb0fa 100644 --- a/src/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/src/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -166,7 +166,6 @@ namespace NzbDrone.Common.Test [TestCase(@"\\server\share\test", @"\\server\share")] public void path_should_return_parent_name_windows(string path, string parentPath) { - WindowsOnly(); path.GetParentName().Should().Be(parentPath); } @@ -175,7 +174,6 @@ namespace NzbDrone.Common.Test [TestCase(@"/test/tv", "test")] public void path_should_return_parent_name_mono(string path, string parentPath) { - PosixOnly(); path.GetParentName().Should().Be(parentPath); } @@ -190,6 +188,7 @@ namespace NzbDrone.Common.Test path.GetDirectoryName().Should().Be(parentPath); } + [TestCase(@"/", "/")] [TestCase(@"/test", "test")] [TestCase(@"/test/tv", "tv")] public void path_should_return_directory_name_mono(string path, string parentPath) diff --git a/src/NzbDrone.Common/Disk/OsPath.cs b/src/NzbDrone.Common/Disk/OsPath.cs index 9593990cd..5b7a4f7ce 100644 --- a/src/NzbDrone.Common/Disk/OsPath.cs +++ b/src/NzbDrone.Common/Disk/OsPath.cs @@ -10,7 +10,7 @@ namespace NzbDrone.Common.Disk private readonly string _path; private readonly OsPathKind _kind; - private static readonly Regex UncPathRegex = new Regex(@"^\\\\(?:\?\\UNC\\)?[^\\]+\\[^\\]+(?:\\|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase); + private static readonly Regex UncPathRegex = new Regex(@"(?^\\\\(?:\?\\UNC\\)?[^\\]+\\[^\\]+)(?:\\|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase); public OsPath(string path) { @@ -22,7 +22,7 @@ namespace NzbDrone.Common.Disk else { _kind = DetectPathKind(path); - _path = TrimTrailingSlashes(FixSlashes(path, _kind), _kind); + _path = FixSlashes(path, _kind); } } @@ -36,7 +36,7 @@ namespace NzbDrone.Common.Disk else { _kind = kind; - _path = TrimTrailingSlashes(FixSlashes(path, kind), kind); + _path = FixSlashes(path, kind); } } @@ -99,7 +99,7 @@ namespace NzbDrone.Common.Disk return path; } - private static string TrimTrailingSlashes(string path, OsPathKind kind) + private static string TrimTrailingSlash(string path, OsPathKind kind) { switch (kind) { @@ -156,7 +156,7 @@ namespace NzbDrone.Common.Disk return Null; } - if (rootLength > index) + if (rootLength > index + 1) { return new OsPath(_path.Substring(0, rootLength)); } @@ -167,6 +167,8 @@ namespace NzbDrone.Common.Disk public string FullPath => _path; + public string PathWithoutTrailingSlash => TrimTrailingSlash(_path, _kind); + public string FileName { get @@ -189,6 +191,30 @@ namespace NzbDrone.Common.Disk } } + public string Name + { + // Meant to behave similar to DirectoryInfo.Name + + get + { + var index = GetFileNameIndex(); + + if (index == -1) + { + return PathWithoutTrailingSlash; + } + + var rootLength = GetRootLength(); + + if (rootLength > index + 1) + { + return _path.Substring(0, rootLength); + } + + return TrimTrailingSlash(_path.Substring(index).TrimStart('/', '\\'), _kind); + } + } + public bool IsValid => _path.IsPathValid(PathValidationType.CurrentOs); private int GetFileNameIndex() @@ -242,7 +268,7 @@ namespace NzbDrone.Common.Disk // \\?\UNC\server\share\ or \\server\share if (uncMatch.Success) { - return uncMatch.Length; + return uncMatch.Groups["unc"].Length; } // \\?\C:\ @@ -345,8 +371,8 @@ namespace NzbDrone.Common.Disk return true; } - var left = _path; - var right = other._path; + var left = PathWithoutTrailingSlash; + var right = other.PathWithoutTrailingSlash; if (Kind == OsPathKind.Windows || other.Kind == OsPathKind.Windows) { diff --git a/src/NzbDrone.Common/Extensions/PathExtensions.cs b/src/NzbDrone.Common/Extensions/PathExtensions.cs index 12f66e9f2..15d58ecd2 100644 --- a/src/NzbDrone.Common/Extensions/PathExtensions.cs +++ b/src/NzbDrone.Common/Extensions/PathExtensions.cs @@ -99,35 +99,23 @@ namespace NzbDrone.Common.Extensions return null; } - var path = new OsPath(cleanPath).Directory.AsDirectory(); + var path = new OsPath(cleanPath).Directory; - return path == OsPath.Null ? null : path.FullPath; + return path == OsPath.Null ? null : path.PathWithoutTrailingSlash; } public static string GetParentName(this string childPath) { - var cleanPath = childPath.GetCleanPath(); + var path = new OsPath(childPath).Directory; - if (cleanPath.IsNullOrWhiteSpace()) - { - return null; - } - - return Directory.GetParent(cleanPath)?.Name; + return path == OsPath.Null ? null : path.Name; } public static string GetDirectoryName(this string childPath) { - var cleanPath = childPath.GetCleanPath(); + var path = new OsPath(childPath); - if (cleanPath.IsNullOrWhiteSpace()) - { - return null; - } - - var directoryInfo = new DirectoryInfo(cleanPath); - - return directoryInfo.Name; + return path == OsPath.Null ? null : path.Name; } public static string GetCleanPath(this string path)