Skip to content

Commit

Permalink
Make Rust bindings work on MacOS
Browse files Browse the repository at this point in the history
Summary:
# Context

At the source control team we are benchmarking various IO workflows on file systems like APFS, BTRFS, and NTFS, as well as using DBs for those workflows instead of native file system of the OS (e.g, RocksDB and LMDB).

See the
```
buck run fbcode/mode/opt fbcode//eden/fs/facebook/prototypes/fsiomicrobench:main
```
tool for more details.

# This diff

The tool is written in Rust and we are adding the LMDB benchmark. We found that the LMDB Rust binding does not compile on MacOS. The issue is that the mode_t is 16 bit on MacOS and is 32 bit on Linux. The Rust bindings always assumed u32 for the `perms` argument.

I have changed the type to u16 as 16 bits can fully represent permissions anyway and converting it to u32 is always safe. While the opposite approach of keeping u32 and and converting to u16 requires partial functions that may fail at run time. That is `.into()` is always safer than `try_into()`.

Welcome alternative suggestions, if there is a better way of doing this.

Reviewed By: nt591

Differential Revision: D68388014

fbshipit-source-id: d50035f398ae7186fef6b96a9858771c15e3d8f4
  • Loading branch information
giorgidze authored and facebook-github-bot committed Jan 20, 2025
1 parent c141726 commit 399dd08
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions hphp/hack/src/utils/lmdb/lmdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ impl Options {
/// SAFETY: Only one instance per canonical path may exist process-wide.
/// Do not have open an LMDB database twice in the same process at the same time.
/// Not even from a plain open() call - close()ing it breaks flock() advisory locking.
pub fn create_file(self, path: impl AsRef<Path>, perms: u32) -> Result<Env> {
pub fn create_file(self, path: impl AsRef<Path>, perms: u16) -> Result<Env> {
let Self { env, flags } = self;
unsafe {
let path = CString::new(path.as_ref().as_os_str().as_bytes())?;
check(mdb_env_open(
env,
path.as_ptr(),
flags | MDB_NOSUBDIR,
perms,
perms.into(),
))?;
std::mem::forget(self); // don't close our MDB_env
Ok(Env {
Expand All @@ -166,15 +166,15 @@ impl Options {
}

/// Open a database environment
pub fn open_file(self, path: impl AsRef<Path>, perms: u32) -> Result<Env> {
pub fn open_file(self, path: impl AsRef<Path>, perms: u16) -> Result<Env> {
let Self { env, flags } = self;
unsafe {
let path = CString::new(path.as_ref().as_os_str().as_bytes())?;
check(mdb_env_open(
env,
path.as_ptr(),
flags | MDB_RDONLY | MDB_NOSUBDIR,
perms,
perms.into(),
))?;
std::mem::forget(self);
Ok(Env {
Expand Down Expand Up @@ -307,11 +307,11 @@ impl Env {
}

/// Change unix permissions on env and lock file
pub fn chmod(&self, mode: u32) -> Result<()> {
pub fn chmod(&self, mode: u16) -> Result<()> {
use std::os::unix::fs::PermissionsExt;
let (path, lockpath) = self.paths()?;
std::fs::set_permissions(path, PermissionsExt::from_mode(mode))?;
std::fs::set_permissions(lockpath, PermissionsExt::from_mode(mode))?;
std::fs::set_permissions(path, PermissionsExt::from_mode(mode.into()))?;
std::fs::set_permissions(lockpath, PermissionsExt::from_mode(mode.into()))?;
Ok(())
}

Expand Down

0 comments on commit 399dd08

Please sign in to comment.