Skip to content

Commit

Permalink
Merge pull request #163 from Sonix/bugfix/163-fix-string-marshalling-…
Browse files Browse the repository at this point in the history
…in-net6

#162: Fix string marshalling for paths in .NET6
  • Loading branch information
CoreyKaylor authored Jul 21, 2023
2 parents c170150 + ba1079a commit fc8e0c0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/LightningDB.Tests/EnvironmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ namespace LightningDB.Tests;
[Collection("SharedFileSystem")]
public class EnvironmentTests : IDisposable
{
private readonly string _path, _pathCopy;
private readonly string _path, _pathCopy, _pathSpecial;
private LightningEnvironment _env;

public EnvironmentTests(SharedFileSystem fileSystem)
{
_path = fileSystem.CreateNewDirectoryForTest();
_pathCopy = fileSystem.CreateNewDirectoryForTest();
_pathSpecial = fileSystem.CreateNewDirectoryForSpecialCharacterTest();
}

public void Dispose()
Expand Down Expand Up @@ -152,6 +153,14 @@ public void CanOpenEnvironmentMoreThan50Mb()

_env.Open();
}

[Fact]
public void CanOpenEnvironmentWithSpecialCharacters()
{
_env = new LightningEnvironment(_pathSpecial);

_env.Open();
}


#if NETCOREAPP3_1_OR_GREATER
Expand Down
7 changes: 7 additions & 0 deletions src/LightningDB.Tests/SharedFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public string CreateNewDirectoryForTest()
Directory.CreateDirectory(path);
return path;
}

public string CreateNewDirectoryForSpecialCharacterTest()
{
var path = Path.Combine(_testTempDir, "Testß", Guid.NewGuid().ToString());
Directory.CreateDirectory(path);
return path;
}
}

[CollectionDefinition("SharedFileSystem")]
Expand Down
26 changes: 23 additions & 3 deletions src/LightningDB/Native/Lmdb.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace LightningDB.Native;

Expand Down Expand Up @@ -214,9 +215,16 @@ public static MDBResultCode mdb_cursor_put(nint cursor, ref MDBValue key, ref Sp
public static extern void mdb_env_close(nint env);

[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern MDBResultCode mdb_env_open(nint env, string path, EnvironmentOpenFlags flags,
private static extern MDBResultCode mdb_env_open(nint env, byte[] path, EnvironmentOpenFlags flags,
UnixAccessMode mode);

internal static MDBResultCode mdb_env_open(nint env, string path, EnvironmentOpenFlags flags,
UnixAccessMode mode)
{
var bytes = Encoding.UTF8.GetBytes(path);
return mdb_env_open(env, bytes, flags, mode);
}

[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_set_mapsize(nint env, nint size);

Expand Down Expand Up @@ -263,10 +271,22 @@ internal static extern MDBResultCode mdb_env_open(nint env, string path, Environ
public static extern MDBResultCode mdb_stat(nint txn, uint dbi, out MDBStat stat);

[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_copy(nint env, string path);
private static extern MDBResultCode mdb_env_copy(nint env, byte[] path);

public static MDBResultCode mdb_env_copy(nint env, string path)
{
var bytes = Encoding.UTF8.GetBytes(path);
return mdb_env_copy(env, bytes);
}

[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopyFlags copyFlags);
private static extern MDBResultCode mdb_env_copy2(nint env, byte[] path, EnvironmentCopyFlags copyFlags);

public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopyFlags copyFlags)
{
var bytes = Encoding.UTF8.GetBytes(path);
return mdb_env_copy2(env, bytes, copyFlags);
}

[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_info(nint env, out MDBEnvInfo stat);
Expand Down

0 comments on commit fc8e0c0

Please sign in to comment.