consolidate path comparsion

This commit is contained in:
Adam Hathcock
2026-05-05 17:02:28 +01:00
parent 3b29de3954
commit 8d6f1014f0
5 changed files with 16 additions and 34 deletions

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using SharpCompress.Common;
using SharpCompress.Readers;
@@ -9,14 +8,6 @@ namespace SharpCompress.Archives;
public static class IArchiveExtensions
{
/// <summary>
/// Gets the appropriate StringComparison for path checks based on the file system.
/// Windows uses case-insensitive file systems, while Unix-like systems use case-sensitive file systems.
/// </summary>
private static StringComparison PathComparison =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
extension(IArchive archive)
{
/// <summary>
@@ -83,7 +74,7 @@ public static class IArchiveExtensions
if (!Directory.Exists(destdir) && seenDirectories.Add(destdir))
{
if (!destdir.StartsWith(fullDestinationDirectoryPath, PathComparison))
if (!destdir.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison))
{
throw new ExtractionException(
"Entry is trying to create a directory outside of the destination directory."

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Common;
@@ -11,14 +10,6 @@ namespace SharpCompress.Archives;
public static class IAsyncArchiveExtensions
{
/// <summary>
/// Gets the appropriate StringComparison for path checks based on the file system.
/// Windows uses case-insensitive file systems, while Unix-like systems use case-sensitive file systems.
/// </summary>
private static StringComparison PathComparison =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
extension(IAsyncArchive archive)
{
/// <summary>
@@ -105,7 +96,7 @@ public static class IAsyncArchiveExtensions
if (!Directory.Exists(destdir) && seenDirectories.Add(destdir))
{
if (!destdir.StartsWith(fullDestinationDirectoryPath, PathComparison))
if (!destdir.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison))
{
throw new ExtractionException(
"Entry is trying to create a directory outside of the destination directory."

View File

@@ -45,7 +45,7 @@ internal static partial class ExtractionMethods
if (!Directory.Exists(destdir))
{
if (!destdir.StartsWith(fullDestinationDirectoryPath, PathComparison))
if (!destdir.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison))
{
throw new ExtractionException(
"Entry is trying to create a directory outside of the destination directory."
@@ -65,7 +65,7 @@ internal static partial class ExtractionMethods
{
destinationFileName = Path.GetFullPath(destinationFileName);
if (!destinationFileName.StartsWith(fullDestinationDirectoryPath, PathComparison))
if (!destinationFileName.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison))
{
throw new ExtractionException(
"Entry is trying to write a file outside of the destination directory."

View File

@@ -1,20 +1,10 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace SharpCompress.Common;
internal static partial class ExtractionMethods
{
/// <summary>
/// Gets the appropriate StringComparison for path checks based on the file system.
/// Windows uses case-insensitive file systems, while Unix-like systems use case-sensitive file systems.
/// </summary>
private static StringComparison PathComparison =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
/// <summary>
/// Extract to specific directory, retaining filename
/// </summary>
@@ -55,7 +45,7 @@ internal static partial class ExtractionMethods
if (!Directory.Exists(destdir))
{
if (!destdir.StartsWith(fullDestinationDirectoryPath, PathComparison))
if (!destdir.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison))
{
throw new ExtractionException(
"Entry is trying to create a directory outside of the destination directory."
@@ -75,7 +65,7 @@ internal static partial class ExtractionMethods
{
destinationFileName = Path.GetFullPath(destinationFileName);
if (!destinationFileName.StartsWith(fullDestinationDirectoryPath, PathComparison))
if (!destinationFileName.StartsWith(fullDestinationDirectoryPath, Utility.PathComparison))
{
throw new ExtractionException(
"Entry is trying to write a file outside of the destination directory."

View File

@@ -3,6 +3,7 @@ using System.Buffers;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -13,6 +14,15 @@ namespace SharpCompress;
internal static partial class Utility
{
/// <summary>
/// Gets the appropriate StringComparison for path checks based on the file system.
/// Windows uses case-insensitive file systems, while Unix-like systems use case-sensitive file systems.
/// </summary>
internal static StringComparison PathComparison =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
public static bool UseSyncOverAsyncDispose()
{
var useSyncOverAsync = false;