Skip to content

Commit

Permalink
Path concatenation fix for .NET Standard
Browse files Browse the repository at this point in the history
Fixes cases where paths were built with mix of \ and / directory delimiter characters on non-Windows systems. This happened with .NET Standard libraries.
  • Loading branch information
LTRData committed Apr 13, 2024
1 parent 037a023 commit 897da6f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Library/DiscUtils.Core/Internal/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ public static string GetFileFromPath(string path)
/// <returns>The combined path.</returns>
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;
}
Expand All @@ -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
}

Expand Down
13 changes: 13 additions & 0 deletions Tests/LibraryTests/Iso9660/IsoFileSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
4 changes: 2 additions & 2 deletions Tests/LibraryTests/LibraryTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ProjectReference Include="..\..\Library\DiscUtils.Core\DiscUtils.Core.csproj" />
<ProjectReference Include="..\..\Library\DiscUtils\DiscUtils.csproj" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework.CompareTo(`net462`))' &lt; 0">
<ItemGroup Condition="'$(TargetFramework.CompareTo(`net462`))' &lt; 0 Or '$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.*" />
<PackageReference Include="System.IO.Compression" Version="4.3.*" />
<PackageReference Include="xunit.extensibility.core" Version="2.6.*" />
Expand All @@ -28,7 +28,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework.CompareTo(`net462`))' &gt;= 0">
<ItemGroup Condition=" '$(TargetFramework.CompareTo(`net462`))' &gt;= 0 And '$(TargetFramework)' != 'netcoreapp3.1'">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="*" />
<PackageReference Include="System.IO.Compression" Version="*" />
<PackageReference Include="xunit.extensibility.core" Version="*" />
Expand Down

0 comments on commit 897da6f

Please sign in to comment.