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..c8a84af1c 100644 --- a/firewood/src/db.rs +++ b/firewood/src/db.rs @@ -31,6 +31,7 @@ use std::{ io::{Cursor, Write}, mem::size_of, num::NonZeroUsize, + os::fd::AsFd, path::Path, sync::Arc, thread::JoinHandle, @@ -407,7 +408,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..96e93c9bd 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() @@ -34,10 +33,10 @@ impl File { .write(true) .mode(0o600) .open(filepath)? - .into_raw_fd()) + .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() @@ -46,7 +45,7 @@ impl File { .write(true) .mode(0o600) .open(filepath)? - .into_raw_fd()) + .into()) } fn _get_fname(fid: u64) -> String { @@ -65,15 +64,13 @@ 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 { + type Target = OwnedFd; + + fn deref(&self) -> &Self::Target { + &self.fd } } diff --git a/firewood/src/storage/buffer.rs b/firewood/src/storage/buffer.rs index f2eeeedb9..8ed871d92 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::{AsFd, AsRawFd}; use std::path::{Path, PathBuf}; use std::rc::Rc; use std::sync::Arc; @@ -227,7 +228,12 @@ 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 +310,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..e535c1706 100644 --- a/firewood/src/storage/mod.rs +++ b/firewood/src/storage/mod.rs @@ -9,6 +9,7 @@ use std::{ fmt::{self, Debug}, num::NonZeroUsize, ops::{Deref, DerefMut}, + os::fd::{AsFd, AsRawFd}, path::PathBuf, sync::Arc, }; @@ -779,7 +780,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 +902,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 +932,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"