|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Security.Cryptography; |
| 5 | +using System.Text; |
| 6 | +using LiteDB.Engine; |
| 7 | + |
| 8 | +namespace LiteDB.Client.Shared; |
| 9 | + |
| 10 | +internal static class SharedMutexNameFactory |
| 11 | +{ |
| 12 | + // Effective Windows limit for named mutexes (conservative). |
| 13 | + private const int WINDOWS_MUTEX_NAME_MAX = 250; |
| 14 | + |
| 15 | + // If the caller adds "Global\" (7 chars) + the name + ".Mutex" (7 chars) to the mutex name, |
| 16 | + // we account for it conservatively here without baking it into the return value. |
| 17 | + // Adjust if your caller prepends something longer. |
| 18 | + private const int CONSERVATIVE_EXTERNAL_PREFIX_LENGTH = 13; // e.g., "Global\\" + name + ".Mutex" |
| 19 | + |
| 20 | + internal static string Create(string fileName, SharedMutexNameStrategy strategy) |
| 21 | + { |
| 22 | + return strategy switch |
| 23 | + { |
| 24 | + SharedMutexNameStrategy.Default => CreateUsingUriEncodingWithFallback(fileName), |
| 25 | + SharedMutexNameStrategy.UriEscape => CreateUsingUriEncoding(fileName), |
| 26 | + SharedMutexNameStrategy.Sha1Hash => CreateUsingSha1(fileName), |
| 27 | + _ => throw new ArgumentOutOfRangeException(nameof(strategy), strategy, null) |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + private static string CreateUsingUriEncodingWithFallback(string fileName) |
| 32 | + { |
| 33 | + var normalized = Normalize(fileName); |
| 34 | + var uri = Uri.EscapeDataString(normalized); |
| 35 | + |
| 36 | + if (IsWindows() && |
| 37 | + uri.Length + CONSERVATIVE_EXTERNAL_PREFIX_LENGTH > WINDOWS_MUTEX_NAME_MAX) |
| 38 | + { |
| 39 | + // Short, stable fallback well under the limit. |
| 40 | + return "sha1-" + ComputeSha1Hex(normalized); |
| 41 | + } |
| 42 | + |
| 43 | + return uri; |
| 44 | + } |
| 45 | + |
| 46 | + private static string CreateUsingUriEncoding(string fileName) |
| 47 | + { |
| 48 | + var normalized = Normalize(fileName); |
| 49 | + var uri = Uri.EscapeDataString(normalized); |
| 50 | + |
| 51 | + if (IsWindows() && |
| 52 | + uri.Length + CONSERVATIVE_EXTERNAL_PREFIX_LENGTH > WINDOWS_MUTEX_NAME_MAX) |
| 53 | + { |
| 54 | + // Fallback to SHA to avoid ArgumentException on Windows. |
| 55 | + return "sha1-" + ComputeSha1Hex(normalized); |
| 56 | + } |
| 57 | + |
| 58 | + return uri; |
| 59 | + } |
| 60 | + |
| 61 | + private static bool IsWindows() |
| 62 | + { |
| 63 | + return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); |
| 64 | + } |
| 65 | + |
| 66 | + internal static string CreateUsingSha1(string value) |
| 67 | + { |
| 68 | + var normalized = Normalize(value); |
| 69 | + return ComputeSha1Hex(normalized); |
| 70 | + } |
| 71 | + |
| 72 | + private static string Normalize(string path) |
| 73 | + { |
| 74 | + // Invariant casing + absolute path yields stable identity. |
| 75 | + return Path.GetFullPath(path).ToLowerInvariant(); |
| 76 | + } |
| 77 | + |
| 78 | + private static string ComputeSha1Hex(string input) |
| 79 | + { |
| 80 | + var data = Encoding.UTF8.GetBytes(input); |
| 81 | + using var sha = SHA1.Create(); |
| 82 | + var hashData = sha.ComputeHash(data); |
| 83 | + |
| 84 | + var sb = new StringBuilder(hashData.Length * 2); |
| 85 | + foreach (var b in hashData) |
| 86 | + { |
| 87 | + sb.Append(b.ToString("X2")); |
| 88 | + } |
| 89 | + |
| 90 | + return sb.ToString(); |
| 91 | + } |
| 92 | +} |
0 commit comments