Skip to content

Commit

Permalink
Upgrade nix
Browse files Browse the repository at this point in the history
Two major changes:
 - features must be included; we only need two (this will speed up
   compiles!)
 - FD safety; we now need an OwnedFd, so attempting to use the fd after
   the object is dropped is no longer valid. Additionally, we don't need
   to implement Drop, as OwnedFd already has a drop which calls close

References:

 * nix-rust/nix#2091 - nix PR for features drop
 * https://doc.rust-lang.org/stable/src/std/os/fd/owned.rs.html#170-182 - OwnedFd docs
 * nix-rust/nix#1906 - nix PR for FD I/O safety
  • Loading branch information
rkuris committed Aug 28, 2023
1 parent 99d97f7 commit f44fed1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 28 deletions.
2 changes: 1 addition & 1 deletion firewood/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions firewood/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
31 changes: 13 additions & 18 deletions firewood/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -25,28 +24,26 @@ impl File {
rootpath: PathBuf,
fname: &str,
options: Options,
) -> Result<Fd, std::io::Error> {
) -> Result<OwnedFd, std::io::Error> {
let mut filepath = rootpath;
filepath.push(fname);
Ok(std::fs::File::options()
.truncate(options == Options::Truncate)
.read(true)
.write(true)
.mode(0o600)
.open(filepath)?
.into_raw_fd())
.open(filepath)?.into())
}

pub fn create_file(rootpath: PathBuf, fname: &str) -> Result<Fd, std::io::Error> {
pub fn create_file(rootpath: PathBuf, fname: &str) -> Result<OwnedFd, std::io::Error> {
let mut filepath = rootpath;
filepath.push(fname);
Ok(std::fs::File::options()
.create(true)
.read(true)
.write(true)
.mode(0o600)
.open(filepath)?
.into_raw_fd())
.open(filepath)?.into())
}

fn _get_fname(fid: u64) -> String {
Expand All @@ -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<PathBuf, std::io::Error> {
Expand Down
5 changes: 3 additions & 2 deletions firewood/src/storage/buffer.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
)
Expand Down
8 changes: 4 additions & 4 deletions firewood/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion growth-ring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f44fed1

Please sign in to comment.