Skip to content

Commit

Permalink
fix: really encode windows paths for mdbx this time (#2806)
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg committed May 24, 2023
1 parent 14a9e5c commit 1e53d5f
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions crates/storage/libmdbx-rs/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ use crate::{
use byteorder::{ByteOrder, NativeEndian};
use libc::c_uint;
use mem::size_of;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(windows)]
use std::os::windows::ffi::OsStrExt;
use std::{
ffi::CString,
fmt,
Expand Down Expand Up @@ -457,9 +453,23 @@ where
}
}

let path = match CString::new(path.as_os_str().as_bytes()) {
#[cfg(unix)]
fn path_to_bytes<P: AsRef<Path>>(path: P) -> Vec<u8> {
use std::os::unix::ffi::OsStrExt;
path.as_ref().as_os_str().as_bytes().to_vec()
}

#[cfg(windows)]
fn path_to_bytes<P: AsRef<Path>>(path: P) -> Vec<u8> {
// On Windows, could use std::os::windows::ffi::OsStrExt to encode_wide(),
// but we end up with a Vec<u16> instead of a Vec<u8>, so that doesn't
// really help.
path.as_ref().to_string_lossy().to_string().into_bytes()
}

let path = match CString::new(path_to_bytes(path)) {
Ok(path) => path,
Err(..) => return Err(crate::Error::Invalid),
Err(_) => return Err(Error::Invalid),
};
mdbx_result(ffi::mdbx_env_open(
env,
Expand Down

0 comments on commit 1e53d5f

Please sign in to comment.