diff --git a/LiteDB/Client/Shared/MutexGenerator.cs b/LiteDB/Client/Shared/MutexGenerator.cs new file mode 100644 index 000000000..45adf3042 --- /dev/null +++ b/LiteDB/Client/Shared/MutexGenerator.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; + +using System.Text; +using System.Threading; +using System.Diagnostics; + +#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER +using System.Security.AccessControl; +using System.Security.Principal; +#endif + +namespace LiteDB.Client.Shared; + +internal static class MutexGenerator +{ +#if NET6_0_OR_GREATER + private static Mutex CreateMutexForNet6OrGreater(string name) + { + if (!OperatingSystem.IsWindows()) + { + return new Mutex(false, "Global\\" + name + ".Mutex"); + } + + var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), + MutexRights.FullControl, AccessControlType.Allow); + + var securitySettings = new MutexSecurity(); + securitySettings.AddAccessRule(allowEveryoneRule); + + return MutexAcl.Create(false, "Global\\" + name + ".Mutex", out _, securitySettings); + } +#endif + +#if NETSTANDARD2_0_OR_GREATER + private static Mutex CreateMutexForNetStandard(string name) + { + var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), + MutexRights.FullControl, AccessControlType.Allow); + + var securitySettings = new MutexSecurity(); + securitySettings.AddAccessRule(allowEveryoneRule); + + var mutex = new Mutex(false, "Global\\" + name + ".Mutex"); + ThreadingAclExtensions.SetAccessControl(mutex, securitySettings); + + return mutex; + } +#endif + +#if NETFRAMEWORK + private static Mutex CreateMutexForNetFramework(string name) + { + var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), + MutexRights.FullControl, AccessControlType.Allow); + + var securitySettings = new MutexSecurity(); + securitySettings.AddAccessRule(allowEveryoneRule); + + return new Mutex(false, "Global\\" + name + ".Mutex", out _, securitySettings); + } +#endif + + public static Mutex CreateMutex(string name) + { +#if NET6_0_OR_GREATER + return CreateMutexForNet6OrGreater(name); +#endif +#if NETSTANDARD2_0_OR_GREATER + return CreateMutexForNetStandard(name); +#endif +#if NETFRAMEWORK + return CreateMutexForNetFramework(name); +#endif + + return new Mutex(false, "Global\\" + name + ".Mutex"); + } +} \ No newline at end of file diff --git a/LiteDB/Client/Shared/SharedEngine.cs b/LiteDB/Client/Shared/SharedEngine.cs index 386679cec..5abd6a16e 100644 --- a/LiteDB/Client/Shared/SharedEngine.cs +++ b/LiteDB/Client/Shared/SharedEngine.cs @@ -5,7 +5,9 @@ using System.Linq; using System.Linq.Expressions; using System.Threading; -#if NETFRAMEWORK +using LiteDB.Client.Shared; + +#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER using System.Security.AccessControl; using System.Security.Principal; #endif @@ -27,17 +29,7 @@ public SharedEngine(EngineSettings settings) try { -#if NETFRAMEWORK - var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), - MutexRights.FullControl, AccessControlType.Allow); - - var securitySettings = new MutexSecurity(); - securitySettings.AddAccessRule(allowEveryoneRule); - - _mutex = new Mutex(false, "Global\\" + name + ".Mutex", out _, securitySettings); -#else - _mutex = new Mutex(false, "Global\\" + name + ".Mutex"); -#endif + _mutex = MutexGenerator.CreateMutex(name); } catch (NotSupportedException ex) { @@ -147,7 +139,7 @@ public bool Rollback() } } - #endregion + #endregion Transaction Operations #region Read Operation @@ -188,7 +180,7 @@ public bool Pragma(string name, BsonValue value) } } - #endregion + #endregion Read Operation #region Write Operations @@ -360,7 +352,7 @@ public bool EnsureIndex(string collection, string name, BsonExpression expressio } } - #endregion + #endregion Write Operations public void Dispose() { diff --git a/LiteDB/LiteDB.csproj b/LiteDB/LiteDB.csproj index cbc9b0446..59b23b5bd 100644 --- a/LiteDB/LiteDB.csproj +++ b/LiteDB/LiteDB.csproj @@ -1,10 +1,10 @@  - net45;netstandard1.3;netstandard2.0 - 5.0.12 - 5.0.12 - 5.0.12 + net45;netstandard1.3;netstandard2.0 + 5.0.14 + 5.0.14 + 5.0.14 Maurício David LiteDB LiteDB - A lightweight embedded .NET NoSQL document store in a single datafile @@ -12,7 +12,7 @@ en-US LiteDB LiteDB - 5.0.12 + 5.0.14 database nosql embedded icon_64x64.png LICENSE @@ -25,9 +25,9 @@ 1.6.1 1701;1702;1705;1591;0618 bin\$(Configuration)\$(TargetFramework)\LiteDB.xml - true - LiteDB.snk true + True + latest