From 663d254ced45079d9f1cb6561c955bfaecfedf57 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Wed, 30 Dec 2015 13:27:17 +0100 Subject: [PATCH] Fixed Ospath incorrectly detecting arbitrary colon as windows path. --- src/NzbDrone.Common.Test/OsPathFixture.cs | 3 +++ src/NzbDrone.Common/Disk/OsPath.cs | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Common.Test/OsPathFixture.cs b/src/NzbDrone.Common.Test/OsPathFixture.cs index ff7a83989..82af442f7 100644 --- a/src/NzbDrone.Common.Test/OsPathFixture.cs +++ b/src/NzbDrone.Common.Test/OsPathFixture.cs @@ -24,6 +24,7 @@ namespace NzbDrone.Common.Test [TestCase("/rooted/linux/path", OsPathKind.Unix)] [TestCase("/", OsPathKind.Unix)] [TestCase("linux/path", OsPathKind.Unix)] + [TestCase(@"Castle:unrooted+linux+path", OsPathKind.Unknown)] public void should_auto_detect_kind(string path, OsPathKind kind) { var result = new OsPath(path); @@ -94,6 +95,8 @@ namespace NzbDrone.Common.Test [TestCase(@"rooted\windows\path")] [TestCase(@"path")] [TestCase("linux/path")] + [TestCase(@"Castle:unrooted+linux+path")] + [TestCase(@"C:unrooted+linux+path")] public void should_detect_unrooted_ospaths(string path) { var osPath = new OsPath(path); diff --git a/src/NzbDrone.Common/Disk/OsPath.cs b/src/NzbDrone.Common/Disk/OsPath.cs index 27d9b4f56..cfd439632 100644 --- a/src/NzbDrone.Common/Disk/OsPath.cs +++ b/src/NzbDrone.Common/Disk/OsPath.cs @@ -44,7 +44,7 @@ namespace NzbDrone.Common.Disk { return OsPathKind.Unix; } - if (path.Contains(':') || path.Contains('\\')) + if (HasWindowsDriveLetter(path) || path.Contains('\\')) { return OsPathKind.Windows; } @@ -55,6 +55,15 @@ namespace NzbDrone.Common.Disk return OsPathKind.Unknown; } + private static bool HasWindowsDriveLetter(string path) + { + if (path.Length < 2) return false; + if (!char.IsLetter(path[0]) || path[1] != ':') return false; + if (path.Length > 2 && path[2] != '\\' && path[2] != '/') return false; + + return true; + } + private static string FixSlashes(string path, OsPathKind kind) { switch (kind) @@ -97,7 +106,7 @@ namespace NzbDrone.Common.Disk { if (IsWindowsPath) { - return _path.StartsWith(@"\\") || _path.Contains(':'); + return _path.StartsWith(@"\\") || HasWindowsDriveLetter(_path); } if (IsUnixPath) {