From da0d0c5a01ba2ca7a1b1ca7f9afbed976687dc46 Mon Sep 17 00:00:00 2001 From: Ori Almog Date: Wed, 29 May 2024 15:14:30 +0100 Subject: [PATCH 1/2] Fix for FileNotFoundException in FileShareDataBusImplementation in cross-platform scenarios Fixes FileNotFoundException in cross-platform scenarios (Windows to Linux or Linux to Windows). The bug was due to incompatible path separators causing exceptions. --- .../DataBus/FileShareDataBusImplementation.cs | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs b/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs index c58f4c8d30..500d2bf24b 100644 --- a/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs +++ b/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs @@ -8,7 +8,33 @@ namespace NServiceBus; using Logging; class FileShareDataBusImplementation : IDataBus -{ +{ + // to account for mixed platforms ie windows -> linux or linux -> windows + internal class PathNormalizer + { + // Example keys + // string key1 = "foldername/filename"; + // string key2 = "foldername\\filename"; + + // Normalize the keys + // string normalizedKey1 = NormalizePath(key1); + // string normalizedKey2 = NormalizePath(key2); + + // Output the normalized keys + // Console.WriteLine(normalizedKey1); // Output will be "foldername\filename" on Windows, "foldername/filename" on Unix-based systems + // Console.WriteLine(normalizedKey2); // Output will be "foldername\filename" on Windows, "foldername/filename" on Unix-based systems + internal static string NormalizePath(string key) + { + // Determine the directory separator for the current platform + char separator = Path.DirectorySeparatorChar; + // Replace any forward slashes (common in URIs) and backward slashes with the platform-specific separator + string normalizedPath = key.Replace('/', separator).Replace('\\', separator); + + return normalizedPath; + } + } + + public FileShareDataBusImplementation(string basePath) { this.basePath = basePath; @@ -18,7 +44,7 @@ public FileShareDataBusImplementation(string basePath) public Task Get(string key, CancellationToken cancellationToken = default) { - var filePath = Path.Combine(basePath, key); + var filePath = Path.Combine(basePath, PathNormalizer.NormalizePath(key)); logger.DebugFormat("Opening stream from '{0}'.", filePath); @@ -71,4 +97,4 @@ string GenerateKey(TimeSpan timeToBeReceived) readonly string basePath; static readonly ILog logger = LogManager.GetLogger(); -} \ No newline at end of file +} From 17a808c634177bc1864c23f2c2bf0077a6c50122 Mon Sep 17 00:00:00 2001 From: Andreas Bednarz Date: Thu, 30 May 2024 14:07:19 +1000 Subject: [PATCH 2/2] Fix formatting --- .../DataBus/FileShareDataBusImplementation.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs b/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs index 500d2bf24b..c7eff58954 100644 --- a/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs +++ b/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs @@ -8,30 +8,30 @@ namespace NServiceBus; using Logging; class FileShareDataBusImplementation : IDataBus -{ +{ // to account for mixed platforms ie windows -> linux or linux -> windows internal class PathNormalizer - { + { // Example keys // string key1 = "foldername/filename"; // string key2 = "foldername\\filename"; - + // Normalize the keys // string normalizedKey1 = NormalizePath(key1); // string normalizedKey2 = NormalizePath(key2); - + // Output the normalized keys // Console.WriteLine(normalizedKey1); // Output will be "foldername\filename" on Windows, "foldername/filename" on Unix-based systems // Console.WriteLine(normalizedKey2); // Output will be "foldername\filename" on Windows, "foldername/filename" on Unix-based systems internal static string NormalizePath(string key) { // Determine the directory separator for the current platform - char separator = Path.DirectorySeparatorChar; + char separator = Path.DirectorySeparatorChar; // Replace any forward slashes (common in URIs) and backward slashes with the platform-specific separator string normalizedPath = key.Replace('/', separator).Replace('\\', separator); - + return normalizedPath; - } + } }