From 0321b3cf258845104ae272c7794be048ddca7b96 Mon Sep 17 00:00:00 2001 From: Sergei Shulepov Date: Tue, 5 Nov 2024 15:18:02 +0100 Subject: [PATCH] chore: rid of fs2 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. --- Cargo.lock | 11 ----------- nomt/Cargo.toml | 1 - nomt/src/store/flock.rs | 11 ++++------- nomt/src/sys/unix.rs | 21 +++++++++++++++++++++ 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3dfdeb5d..0a2613ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -418,16 +418,6 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" -[[package]] -name = "fs2" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "funty" version = "2.0.0" @@ -698,7 +688,6 @@ dependencies = [ "crossbeam", "crossbeam-channel", "dashmap", - "fs2", "fxhash", "hex", "hex-literal", diff --git a/nomt/Cargo.toml b/nomt/Cargo.toml index f5eef40f..1180d465 100644 --- a/nomt/Cargo.toml +++ b/nomt/Cargo.toml @@ -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] diff --git a/nomt/src/store/flock.rs b/nomt/src/store/flock.rs index b562fa0c..cbfe7b23 100644 --- a/nomt/src/store/flock.rs +++ b/nomt/src/store/flock.rs @@ -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, @@ -22,11 +20,10 @@ 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}"); } } } @@ -34,7 +31,7 @@ impl Flock { 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}"); } } diff --git a/nomt/src/sys/unix.rs b/nomt/src/sys/unix.rs index d85b218f..fd7c15dd 100644 --- a/nomt/src/sys/unix.rs +++ b/nomt/src/sys/unix.rs @@ -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(()) + } + } +}