Skip to content

Commit

Permalink
add fsync / fdatasync operations
Browse files Browse the repository at this point in the history
  • Loading branch information
problame committed Jan 17, 2024
1 parent 0dd3a2f commit 381f1aa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
54 changes: 54 additions & 0 deletions tokio-epoll-uring/src/ops/fsync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::os::fd::AsRawFd;

use uring_common::{
io_fd::IoFd,
io_uring::{self},
};

use crate::system::submission::op_fut::Op;

pub struct FsyncOp<F>
where
F: IoFd + Send,
{
pub(crate) file: F,
pub(crate) flags: io_uring::types::FsyncFlags,
}

impl<F> crate::sealed::Sealed for FsyncOp<F> where F: IoFd + Send {}

impl<F> Op for FsyncOp<F>
where
F: IoFd + Send,
{
type Resources = F;
type Success = ();
type Error = std::io::Error;

fn make_sqe(&mut self) -> io_uring::squeue::Entry {
io_uring::opcode::Fsync::new(io_uring::types::Fd(
// SAFETY: we hold `F` in self, and if `self` is dropped, we hand the fd to the
// `System` to keep it live until the operation completes.
#[allow(unused_unsafe)]
unsafe {
self.file.as_fd().as_raw_fd()
},
))
.flags(self.flags)
.build()
}

fn on_failed_submission(self) -> Self::Resources {
self.file
}

fn on_op_completion(self, res: i32) -> (Self::Resources, Result<Self::Success, Self::Error>) {
// https://man.archlinux.org/man/extra/liburing/io_uring_prep_fsync.3.en
let res = if res < 0 {
Err(std::io::Error::from_raw_os_error(-res))
} else {
Ok(())
};
(self.file, res)
}
}
1 change: 1 addition & 0 deletions tokio-epoll-uring/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub use crate::system::submission::op_fut::Op;
pub mod nop;
pub mod open_at;
pub mod read;
pub mod fsync;
19 changes: 18 additions & 1 deletion tokio-epoll-uring/src/system/lifecycle/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{os::fd::OwnedFd, path::Path, task::ready};
use uring_common::{buf::BoundedBufMut, io_fd::IoFd};

use crate::{
ops::{open_at::OpenAtOp, read::ReadOp},
ops::{open_at::OpenAtOp, read::ReadOp, fsync::FsyncOp},
system::submission::{op_fut::execute_op, SubmitSide},
};

Expand Down Expand Up @@ -141,4 +141,21 @@ impl crate::SystemHandle {
res
})
}

pub async fn fsync<F: IoFd + Send>(&self, file: F) -> (F, Result<(), crate::system::submission::op_fut::Error<std::io::Error>>) {
let op = FsyncOp {
file, flags: uring_common::io_uring::types::FsyncFlags::empty(),
};
let inner = self.inner.as_ref().unwrap();
execute_op(op, inner.submit_side.weak(), None).await
}

pub async fn fdatasync<F: IoFd + Send>(&self, file: F) -> (F, Result<(), crate::system::submission::op_fut::Error<std::io::Error>>) {
let op = FsyncOp {
file, flags: uring_common::io_uring::types::FsyncFlags::DATASYNC,
};
let inner = self.inner.as_ref().unwrap();
execute_op(op, inner.submit_side.weak(), None).await
}

}

0 comments on commit 381f1aa

Please sign in to comment.