diff --git a/mdbx/env.go b/mdbx/env.go index 9510fed..189aba7 100644 --- a/mdbx/env.go +++ b/mdbx/env.go @@ -173,34 +173,6 @@ func (env *Env) SetStrictThreadMode(mode bool) { var errNotOpen = errors.New("enivornment is not open") -/* TODO: fix error: cannot convert *mf (variable of type _Ctype_HANDLE) to type uintptr - -// FD returns the open file descriptor (or Windows file handle) for the given -// environment. An error is returned if the environment has not been -// successfully Opened (where C API just retruns an invalid handle). -// -// See mdbx_env_get_fd. -func (env *Env) FD() (uintptr, error) { - // fdInvalid is the value -1 as a uintptr, which is used by MDBX in the - // case that env has not been opened yet. the strange construction is done - // to avoid constant value overflow errors at compile time. - const fdInvalid = ^uintptr(0) - - mf := new(C.mdbx_filehandle_t) - ret := C.mdbx_env_get_fd(env._env, mf) - err := operrno("mdbx_env_get_fd", ret) - if err != nil { - return 0, err - } - fd := uintptr(*mf) - - if fd == fdInvalid { - return 0, errNotOpen - } - return fd, nil -} -*/ - // ReaderList dumps the contents of the reader lock table as text. Readers // start on the second line as space-delimited fields described by the first // line. diff --git a/mdbx/env_not_win.go b/mdbx/env_not_win.go index 9ea0d74..5ca8ee9 100644 --- a/mdbx/env_not_win.go +++ b/mdbx/env_not_win.go @@ -24,3 +24,28 @@ import "C" // } // return C.GoString(cpath), nil //} + +// FD returns the open file descriptor (or Windows file handle) for the given +// environment. An error is returned if the environment has not been +// successfully Opened (where C API just retruns an invalid handle). +// +// See mdbx_env_get_fd. +func (env *Env) FD() (uintptr, error) { + // fdInvalid is the value -1 as a uintptr, which is used by MDBX in the + // case that env has not been opened yet. the strange construction is done + // to avoid constant value overflow errors at compile time. + const fdInvalid = ^uintptr(0) + + mf := new(C.mdbx_filehandle_t) + ret := C.mdbx_env_get_fd(env._env, mf) + err := operrno("mdbx_env_get_fd", ret) + if err != nil { + return 0, err + } + fd := uintptr(*mf) + + if fd == fdInvalid { + return 0, errNotOpen + } + return fd, nil +} diff --git a/mdbx/env_test.go b/mdbx/env_test.go index dfec034..dfb6642 100644 --- a/mdbx/env_test.go +++ b/mdbx/env_test.go @@ -2,6 +2,8 @@ package mdbx import ( "fmt" + "runtime" + "strings" "testing" ) @@ -99,12 +101,11 @@ func TestEnv_PreOpen(t *testing.T) { } -/* func TestEnv_FD(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("FD funcs not supported on windows") } - env, err1 := NewEnv() + env, err1 := NewEnv(Default) if err1 != nil { t.Error(err1) return @@ -131,7 +132,6 @@ func TestEnv_FD(t *testing.T) { t.Errorf("fd: %x", fd) } } -*/ func TestEnv_Flags(t *testing.T) { env, _ := setup(t) diff --git a/mdbx/env_windows.go b/mdbx/env_windows.go index 6b55881..3771238 100644 --- a/mdbx/env_windows.go +++ b/mdbx/env_windows.go @@ -4,6 +4,7 @@ package mdbx #include "mdbxgo.h" */ import "C" +import "unsafe" // TODO: fix me please //func (env *Env) Path() (string, error) { @@ -17,3 +18,28 @@ import "C" // } // return C.GoString(cpath), nil //} + +// FD returns the open file descriptor (or Windows file handle) for the given +// environment. An error is returned if the environment has not been +// successfully Opened (where C API just retruns an invalid handle). +// +// See mdbx_env_get_fd. +func (env *Env) FD() (uintptr, error) { + // fdInvalid is the value -1 as a uintptr, which is used by MDBX in the + // case that env has not been opened yet. the strange construction is done + // to avoid constant value overflow errors at compile time. + const fdInvalid = ^uintptr(0) + + var fh C.mdbx_filehandle_t + ret := C.mdbx_env_get_fd(env._env, &fh) + err := operrno("mdbx_env_get_fd", ret) + if err != nil { + return 0, err + } + fd := uintptr(unsafe.Pointer(fh)) + + if fd == fdInvalid { + return 0, errNotOpen + } + return fd, nil +}