Skip to content

Commit

Permalink
chore: rid of fs2
Browse files Browse the repository at this point in the history
fs2 was introduced in PR #500, but we actually don't really need that
much from it. This commit removes the extra dependency in favor of doing
the same directly. I think this is pretty-much equivalent to inlining
the code.
  • Loading branch information
pepyakin committed Nov 5, 2024
1 parent 5fabe7b commit 0321b3c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion nomt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ lru = "0.12.3"
libc = "0.2.155"
criterion = { version = "0.3", optional = true }
thread_local = "1.1.8"
fs2 = "0.4.3"
cfg-if = "1.0.0"

[target.'cfg(target_os="linux")'.dependencies]
Expand Down
11 changes: 4 additions & 7 deletions nomt/src/store/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use std::{
path::Path,
};

use fs2::FileExt as _;

/// Represents a cross-platform advisory lock on a directory.
pub struct Flock {
lock_fd: File,
Expand All @@ -22,19 +20,18 @@ impl Flock {
.create(true)
.open(lock_path)?;

match lock_fd.try_lock_exclusive() {
match crate::sys::unix::try_lock_exclusive(&lock_fd) {
Ok(_) => Ok(Self { lock_fd }),
Err(_) => {
let err = fs2::lock_contended_error();
anyhow::bail!("Failed to lock directory: {err}");
Err(e) => {
anyhow::bail!("Failed to lock directory: {e}");
}
}
}
}

impl Drop for Flock {
fn drop(&mut self) {
if let Err(e) = self.lock_fd.unlock() {
if let Err(e) = crate::sys::unix::unlock(&self.lock_fd) {
eprintln!("Failed to unlock directory lock: {e}");
}
}
Expand Down
21 changes: 21 additions & 0 deletions nomt/src/sys/unix.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
//! Common Unix definitions.
use std::{fs::File, os::fd::AsRawFd as _};

pub fn try_lock_exclusive(file: &File) -> std::io::Result<()> {
unsafe {
if libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) == -1 {
Err(std::io::Error::last_os_error())
} else {
Ok(())
}
}
}

pub fn unlock(file: &File) -> std::io::Result<()> {
unsafe {
if libc::flock(file.as_raw_fd(), libc::LOCK_UN) == -1 {
Err(std::io::Error::last_os_error())
} else {
Ok(())
}
}
}

0 comments on commit 0321b3c

Please sign in to comment.