From 897da6f67744355c5bbc0084936490ebd9f32a08 Mon Sep 17 00:00:00 2001
From: LTRData
Date: Sat, 13 Apr 2024 21:42:24 +0200
Subject: [PATCH] Path concatenation fix for .NET Standard
Fixes cases where paths were built with mix of \ and / directory delimiter characters on non-Windows systems. This happened with .NET Standard libraries.
---
Library/DiscUtils.Core/Internal/Utilities.cs | 6 +++---
Tests/LibraryTests/Iso9660/IsoFileSystemTest.cs | 13 +++++++++++++
Tests/LibraryTests/LibraryTests.csproj | 4 ++--
3 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/Library/DiscUtils.Core/Internal/Utilities.cs b/Library/DiscUtils.Core/Internal/Utilities.cs
index a0a8eab24..5f843d4a5 100644
--- a/Library/DiscUtils.Core/Internal/Utilities.cs
+++ b/Library/DiscUtils.Core/Internal/Utilities.cs
@@ -207,10 +207,10 @@ public static string GetFileFromPath(string path)
/// The combined path.
public static string CombinePaths(string a, string b)
{
-#if NETCOREAPP
+#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP
return Path.Combine(a, b);
#else
- if (string.IsNullOrEmpty(a) || (b.Length > 0 && b[0] == '\\'))
+ if (string.IsNullOrEmpty(a) || (b.Length > 0 && b[0] is '\\' or '/'))
{
return b;
}
@@ -220,7 +220,7 @@ public static string CombinePaths(string a, string b)
return a;
}
- return a.TrimEnd('\\') + '\\' + b.TrimStart('\\');
+ return a.TrimEnd(PathSeparators) + Path.DirectorySeparatorChar + b.TrimStart(PathSeparators);
#endif
}
diff --git a/Tests/LibraryTests/Iso9660/IsoFileSystemTest.cs b/Tests/LibraryTests/Iso9660/IsoFileSystemTest.cs
index 765655014..fb65fc269 100644
--- a/Tests/LibraryTests/Iso9660/IsoFileSystemTest.cs
+++ b/Tests/LibraryTests/Iso9660/IsoFileSystemTest.cs
@@ -77,6 +77,19 @@ public void Root()
Assert.Null(fs.Root.Parent);
}
+ [Fact]
+ public void Dirs()
+ {
+ var dir1 = Path.DirectorySeparatorChar + "DIR1";
+ var dir2 = Path.DirectorySeparatorChar + "DIR2";
+
+ var builder = new CDBuilder();
+ builder.AddDirectory(dir1);
+ builder.AddDirectory(dir2);
+ var fs = new CDReader(builder.Build(), true);
+ Assert.Equal(fs.GetDirectories(""), [dir1, dir2]);
+ }
+
[Fact]
public void LargeDirectory()
{
diff --git a/Tests/LibraryTests/LibraryTests.csproj b/Tests/LibraryTests/LibraryTests.csproj
index 4c019a5de..b754a2783 100644
--- a/Tests/LibraryTests/LibraryTests.csproj
+++ b/Tests/LibraryTests/LibraryTests.csproj
@@ -18,7 +18,7 @@
-
+
@@ -28,7 +28,7 @@
runtime; build; native; contentfiles; analyzers; buildtransitive
-
+