Skip to content

Commit 3baba6c

Browse files
committed
clean-up static FdbNative ctor to support osx and linux
1 parent 26667b0 commit 3baba6c

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
@@ -49,7 +49,7 @@ internal static unsafe class FdbNative
4949
private const string FDB_C_DLL = "libfdb_c.so";
5050
#else
5151
/// <summary>Name of the C API dll used for P/Invoking</summary>
52-
private const string FDB_C_DLL = "fdb_c.dll";
52+
private const string FDB_C_DLL = "fdb_c";
5353
#endif
5454

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

243244
var libraryPath = Fdb.Options.NativeLibPath;
244-
if (libraryPath != null)
245+
246+
if (libraryPath == null)
247+
{
248+
return;
249+
}
250+
251+
try
245252
{
246-
try
253+
if (libraryPath.Length == 0)
254+
{ // CLR will handle the search
255+
libraryPath = FDB_C_DLL;
256+
}
257+
else
247258
{
248-
if (libraryPath.Length == 0)
249-
{ // CLR will handle the search
250-
libraryPath = FDB_C_DLL;
251-
}
252-
else if (!libraryPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
253-
{ // add the file name
254-
libraryPath = Path.Combine(Fdb.Options.NativeLibPath, FDB_C_DLL);
259+
var fileName = Path.GetFileName(libraryPath);
260+
if (String.IsNullOrEmpty(fileName))
261+
{
262+
libraryPath = Path.Combine(libraryPath, FDB_C_DLL);
255263
}
264+
}
256265

257-
FdbCLib = UnmanagedLibrary.Load(libraryPath);
266+
FdbCLib = UnmanagedLibrary.Load(libraryPath);
267+
}
268+
catch (Exception e)
269+
{
270+
if (FdbCLib != null) FdbCLib.Dispose();
271+
FdbCLib = null;
272+
if (e is BadImageFormatException && IntPtr.Size == 4)
273+
{
274+
e = new InvalidOperationException("The native FDB client is 64-bit only, and cannot be loaded in a 32-bit process.", e);
258275
}
259-
catch (Exception e)
276+
else
260277
{
261-
if (FdbCLib != null) FdbCLib.Dispose();
262-
FdbCLib = null;
263-
if (e is BadImageFormatException && IntPtr.Size == 4)
264-
{
265-
e = new InvalidOperationException("The native FDB client is 64-bit only, and cannot be loaded in a 32-bit process.", e);
266-
}
267-
else
268-
{
269-
e = new InvalidOperationException("An error occurred while loading the native FoundationDB library", e);
270-
}
271-
LibraryLoadError = ExceptionDispatchInfo.Capture(e);
278+
e = new InvalidOperationException("An error occurred while loading the native FoundationDB library", e);
272279
}
280+
LibraryLoadError = ExceptionDispatchInfo.Capture(e);
273281
}
282+
274283
}
275284

276285
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)