diff --git a/firewood/Cargo.toml b/firewood/Cargo.toml index aad6434e0..989e7401d 100644 --- a/firewood/Cargo.toml +++ b/firewood/Cargo.toml @@ -27,7 +27,7 @@ futures = "0.3.24" hex = "0.4.3" lru = "0.11.0" metered = "0.9.0" -nix = "0.26.1" +nix = {version = "0.27.1", features = ["fs", "uio"]} parking_lot = "0.12.1" primitive-types = { version = "0.12.0", features = ["impl-rlp"] } rlp = "0.5.2" diff --git a/firewood/src/db.rs b/firewood/src/db.rs index 30520a575..770fdf05f 100644 --- a/firewood/src/db.rs +++ b/firewood/src/db.rs @@ -33,7 +33,7 @@ use std::{ num::NonZeroUsize, path::Path, sync::Arc, - thread::JoinHandle, + thread::JoinHandle, os::fd::AsFd, }; const MERKLE_META_SPACE: SpaceId = 0x0; @@ -407,7 +407,7 @@ impl Db { let root_hash_path = file::touch_dir("root_hash", &db_path)?; let file0 = crate::file::File::new(0, SPACE_RESERVED, &merkle_meta_path)?; - let fd0 = file0.get_fd(); + let fd0 = file0.as_fd(); if reset { // initialize dbparams diff --git a/firewood/src/file.rs b/firewood/src/file.rs index a23f43dbd..4e3fb183d 100644 --- a/firewood/src/file.rs +++ b/firewood/src/file.rs @@ -3,15 +3,14 @@ // Copied from CedrusDB -use std::os::fd::IntoRawFd; -pub(crate) use std::os::unix::io::RawFd as Fd; +use std::ops::Deref; +use std::os::fd::OwnedFd; + use std::path::{Path, PathBuf}; use std::{io::ErrorKind, os::unix::prelude::OpenOptionsExt}; -use nix::unistd::close; - pub struct File { - fd: Fd, + fd: OwnedFd, } #[derive(PartialEq, Eq)] @@ -25,7 +24,7 @@ impl File { rootpath: PathBuf, fname: &str, options: Options, - ) -> Result { + ) -> Result { let mut filepath = rootpath; filepath.push(fname); Ok(std::fs::File::options() @@ -33,11 +32,10 @@ impl File { .read(true) .write(true) .mode(0o600) - .open(filepath)? - .into_raw_fd()) + .open(filepath)?.into()) } - pub fn create_file(rootpath: PathBuf, fname: &str) -> Result { + pub fn create_file(rootpath: PathBuf, fname: &str) -> Result { let mut filepath = rootpath; filepath.push(fname); Ok(std::fs::File::options() @@ -45,8 +43,7 @@ impl File { .read(true) .write(true) .mode(0o600) - .open(filepath)? - .into_raw_fd()) + .open(filepath)?.into()) } fn _get_fname(fid: u64) -> String { @@ -65,16 +62,14 @@ impl File { }; Ok(File { fd }) } - - pub fn get_fd(&self) -> Fd { - self.fd - } } -impl Drop for File { - fn drop(&mut self) { - close(self.fd).unwrap(); +impl Deref for File { + fn deref(&self) -> &Self::Target { + &self.fd } + + type Target = OwnedFd; } pub fn touch_dir(dirname: &str, rootdir: &Path) -> Result { diff --git a/firewood/src/storage/buffer.rs b/firewood/src/storage/buffer.rs index f2eeeedb9..4d27ed3b7 100644 --- a/firewood/src/storage/buffer.rs +++ b/firewood/src/storage/buffer.rs @@ -1,6 +1,7 @@ //! Disk buffer for staging in memory pages and flushing them to disk. use std::fmt::Debug; use std::ops::IndexMut; +use std::os::fd::{AsRawFd, AsFd}; use std::path::{Path, PathBuf}; use std::rc::Rc; use std::sync::Arc; @@ -227,7 +228,7 @@ fn schedule_write( .unwrap() .get_file(fid) .unwrap(); - aiomgr.write(file.get_fd(), offset & fmask, p.staging_data.clone(), None) + aiomgr.write(file.as_raw_fd(), offset & fmask, p.staging_data.clone(), None) }; let task = { @@ -304,7 +305,7 @@ async fn init_wal( e, final_path )) })? - .get_fd(), + .as_fd(), &redo.data, (offset & file_mask) as nix::libc::off_t, ) diff --git a/firewood/src/storage/mod.rs b/firewood/src/storage/mod.rs index b79f91702..f4b602441 100644 --- a/firewood/src/storage/mod.rs +++ b/firewood/src/storage/mod.rs @@ -10,7 +10,7 @@ use std::{ num::NonZeroUsize, ops::{Deref, DerefMut}, path::PathBuf, - sync::Arc, + sync::Arc, os::fd::{AsFd, AsRawFd}, }; use thiserror::Error; use tokio::sync::{mpsc::error::SendError, oneshot::error::RecvError}; @@ -779,7 +779,7 @@ impl CachedSpaceInner { let mut page: Page = Page::new([0; PAGE_SIZE as usize]); nix::sys::uio::pread( - file.get_fd(), + file.as_fd(), page.deref_mut(), (poff & (file_size - 1)) as nix::libc::off_t, ) @@ -901,7 +901,7 @@ impl FilePool { rootdir: rootdir.to_path_buf(), }; let f0 = s.get_file(0)?; - if flock(f0.get_fd(), FlockArg::LockExclusiveNonblock).is_err() { + if flock(f0.as_raw_fd(), FlockArg::LockExclusiveNonblock).is_err() { return Err(StoreError::Init("the store is busy".into())); } Ok(s) @@ -931,7 +931,7 @@ impl FilePool { impl Drop for FilePool { fn drop(&mut self) { let f0 = self.get_file(0).unwrap(); - flock(f0.get_fd(), FlockArg::UnlockNonblock).ok(); + flock(f0.as_raw_fd(), FlockArg::UnlockNonblock).ok(); } } diff --git a/growth-ring/Cargo.toml b/growth-ring/Cargo.toml index f17abd50b..5de0781f2 100644 --- a/growth-ring/Cargo.toml +++ b/growth-ring/Cargo.toml @@ -15,7 +15,7 @@ scan_fmt = "0.2.6" regex = "1.6.0" async-trait = "0.1.57" futures = "0.3.24" -nix = "0.26.2" +nix = {version = "0.27.1", features = ["fs", "uio"]} libc = "0.2.133" bytemuck = {version = "1.13.1", features = ["derive"]} thiserror = "1.0.40"