From 906200318abb9670c8ef8f55535886b6eba3e6bf Mon Sep 17 00:00:00 2001 From: Ori Almog Date: Wed, 29 May 2024 15:14:30 +0100 Subject: [PATCH] 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 | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs b/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs index 8efa93d14b..f9bd1d2b4f 100644 --- a/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs +++ b/src/NServiceBus.Core/DataBus/FileShareDataBusImplementation.cs @@ -9,6 +9,32 @@ namespace NServiceBus 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);