Skip to content

Commit 9832934

Browse files
committed
clean-up static FdbNative ctor to support osx and linux
1 parent f15295a commit 9832934

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

FoundationDB.Client/Native/FdbNative.cs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal static unsafe class FdbNative
4848
private const string FDB_C_DLL = "libfdb_c.so";
4949
#else
5050
/// <summary>Name of the C API dll used for P/Invoking</summary>
51-
private const string FDB_C_DLL = "fdb_c.dll";
51+
private const string FDB_C_DLL = "fdb_c";
5252
#endif
5353

5454
/// <summary>Handle on the native FDB C API library</summary>
@@ -236,40 +236,49 @@ static FdbNative()
236236
{
237237
// Impact of NativeLibPath:
238238
// - If null, don't preload the library, and let the CLR find the file using the default P/Invoke behavior
239-
// - If String.Empty, call win32 LoadLibrary("fdb_c.dll") and let the os find the file (using the standard OS behavior)
240-
// - Else, combine the path with "fdb_c.dll" and call LoadLibrary with the resulting (relative or absolute) path
239+
// - If String.Empty, call win32 LoadLibrary(FDB_C_DLL) and let the os find the file (using the standard OS behavior)
240+
// - If path is folder, append the FDB_C_DLL
241+
// Afterwards - call LoadLibrary with the resulting (relative or absolute) path
241242

242243
var libraryPath = Fdb.Options.NativeLibPath;
243-
if (libraryPath != null)
244+
245+
if (libraryPath == null)
246+
{
247+
return;
248+
}
249+
250+
try
244251
{
245-
try
252+
if (libraryPath.Length == 0)
253+
{ // CLR will handle the search
254+
libraryPath = FDB_C_DLL;
255+
}
256+
else
246257
{
247-
if (libraryPath.Length == 0)
248-
{ // CLR will handle the search
249-
libraryPath = FDB_C_DLL;
250-
}
251-
else if (!libraryPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
252-
{ // add the file name
253-
libraryPath = Path.Combine(Fdb.Options.NativeLibPath, FDB_C_DLL);
258+
var fileName = Path.GetFileName(libraryPath);
259+
if (String.IsNullOrEmpty(fileName))
260+
{
261+
libraryPath = Path.Combine(libraryPath, FDB_C_DLL);
254262
}
263+
}
255264

256-
FdbCLib = UnmanagedLibrary.Load(libraryPath);
265+
FdbCLib = UnmanagedLibrary.Load(libraryPath);
266+
}
267+
catch (Exception e)
268+
{
269+
if (FdbCLib != null) FdbCLib.Dispose();
270+
FdbCLib = null;
271+
if (e is BadImageFormatException && IntPtr.Size == 4)
272+
{
273+
e = new InvalidOperationException("The native FDB client is 64-bit only, and cannot be loaded in a 32-bit process.", e);
257274
}
258-
catch (Exception e)
275+
else
259276
{
260-
if (FdbCLib != null) FdbCLib.Dispose();
261-
FdbCLib = null;
262-
if (e is BadImageFormatException && IntPtr.Size == 4)
263-
{
264-
e = new InvalidOperationException("The native FDB client is 64-bit only, and cannot be loaded in a 32-bit process.", e);
265-
}
266-
else
267-
{
268-
e = new InvalidOperationException("An error occurred while loading the native FoundationDB library", e);
269-
}
270-
LibraryLoadError = ExceptionDispatchInfo.Capture(e);
277+
e = new InvalidOperationException("An error occurred while loading the native FoundationDB library", e);
271278
}
279+
LibraryLoadError = ExceptionDispatchInfo.Capture(e);
272280
}
281+
273282
}
274283

275284
private static void EnsureLibraryIsLoaded()

FoundationDB.Client/Native/UnmanagedLibrary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public static bool FreePlatformLibrary(IntPtr hModule)
117117
{
118118
return FreeLibrary(hModule);
119119
}
120-
return return dlclose(hModule) == 0;
120+
return dlclose(hModule) == 0;
121121
}
122122
#endif
123123
}

0 commit comments

Comments
 (0)