#[allow(unreachable_pub, unused_imports, dead_code)]
#[path = "../poll/fs.rs"]
mod poll;
#[allow(unreachable_pub, unused_imports, dead_code)]
#[path = "../fs/mod.rs"]
mod uring;
use std::{io, path::Path};
use crate::buf::{BufResult, IoBuf, IoBufMut};
pub use crate::fs_ops::*;
pub enum File {
Uring(uring::File),
Poll(poll::File),
}
impl File {
pub async fn open(path: impl AsRef<Path>) -> io::Result<Self> {
if crate::reactor::is_uring() {
uring::File::open(path).await.map(File::Uring)
} else {
poll::File::open(path).await.map(File::Poll)
}
}
pub async fn create(path: impl AsRef<Path>) -> io::Result<Self> {
if crate::reactor::is_uring() {
uring::File::create(path).await.map(File::Uring)
} else {
poll::File::create(path).await.map(File::Poll)
}
}
pub fn from_std(file: std::fs::File) -> Self {
if crate::reactor::is_uring() {
File::Uring(uring::File::from_std(file))
} else {
File::Poll(poll::File::from_std(file))
}
}
pub async fn read_at<B>(&self, buf: B, pos: u64) -> BufResult<usize, B>
where
B: IoBufMut + Send + 'static,
{
match self {
File::Uring(f) => f.read_at(buf, pos).await,
File::Poll(f) => f.read_at(buf, pos).await,
}
}
pub async fn read_exact_at<B>(&self, buf: B, pos: u64) -> BufResult<(), B>
where
B: IoBufMut + Send + 'static,
{
match self {
File::Uring(f) => f.read_exact_at(buf, pos).await,
File::Poll(f) => f.read_exact_at(buf, pos).await,
}
}
pub async fn write_at<B>(&self, buf: B, pos: u64) -> BufResult<usize, B>
where
B: IoBuf + Send + 'static,
{
match self {
File::Uring(f) => f.write_at(buf, pos).await,
File::Poll(f) => f.write_at(buf, pos).await,
}
}
pub async fn write_all_at<B>(&self, buf: B, pos: u64) -> BufResult<(), B>
where
B: IoBuf + IoBufMut + Send + 'static,
{
match self {
File::Uring(f) => f.write_all_at(buf, pos).await,
File::Poll(f) => f.write_all_at(buf, pos).await,
}
}
pub async fn read_vectored_at<B>(&self, bufs: Vec<B>, pos: u64) -> BufResult<usize, Vec<B>>
where
B: IoBufMut + Send + 'static,
{
match self {
File::Uring(f) => f.read_vectored_at(bufs, pos).await,
File::Poll(f) => f.read_vectored_at(bufs, pos).await,
}
}
pub async fn write_vectored_at<B>(&self, bufs: Vec<B>, pos: u64) -> BufResult<usize, Vec<B>>
where
B: IoBuf + Send + 'static,
{
match self {
File::Uring(f) => f.write_vectored_at(bufs, pos).await,
File::Poll(f) => f.write_vectored_at(bufs, pos).await,
}
}
pub async fn write_all_vectored_at<B>(&self, bufs: Vec<B>, pos: u64) -> BufResult<(), Vec<B>>
where
B: IoBuf + Send + 'static,
{
match self {
File::Uring(f) => f.write_all_vectored_at(bufs, pos).await,
File::Poll(f) => f.write_all_vectored_at(bufs, pos).await,
}
}
pub async fn sync_all(&self) -> io::Result<()> {
match self {
File::Uring(f) => f.sync_all().await,
File::Poll(f) => f.sync_all().await,
}
}
pub async fn sync_data(&self) -> io::Result<()> {
match self {
File::Uring(f) => f.sync_data().await,
File::Poll(f) => f.sync_data().await,
}
}
pub async fn metadata(&self) -> io::Result<std::fs::Metadata> {
match self {
File::Uring(f) => f.metadata().await,
File::Poll(f) => f.metadata().await,
}
}
pub async fn set_len(&self, size: u64) -> io::Result<()> {
match self {
File::Uring(f) => f.set_len(size).await,
File::Poll(f) => f.set_len(size).await,
}
}
pub async fn set_permissions(&self, perm: std::fs::Permissions) -> io::Result<()> {
match self {
File::Uring(f) => f.set_permissions(perm).await,
File::Poll(f) => f.set_permissions(perm).await,
}
}
pub async fn close(self) -> io::Result<()> {
match self {
File::Uring(f) => f.close().await,
File::Poll(f) => f.close().await,
}
}
}
#[derive(Debug, Clone)]
pub struct OpenOptions {
read: bool,
write: bool,
append: bool,
truncate: bool,
create: bool,
create_new: bool,
noatime: bool,
mode: u32,
}
impl Default for OpenOptions {
fn default() -> Self {
Self::new()
}
}
impl OpenOptions {
pub fn new() -> Self {
Self {
read: false,
write: false,
append: false,
truncate: false,
create: false,
create_new: false,
noatime: false,
mode: 0o666,
}
}
pub fn read(&mut self, read: bool) -> &mut Self {
self.read = read;
self
}
pub fn write(&mut self, write: bool) -> &mut Self {
self.write = write;
self
}
pub fn append(&mut self, append: bool) -> &mut Self {
self.append = append;
self
}
pub fn truncate(&mut self, truncate: bool) -> &mut Self {
self.truncate = truncate;
self
}
pub fn create(&mut self, create: bool) -> &mut Self {
self.create = create;
self
}
pub fn create_new(&mut self, create_new: bool) -> &mut Self {
self.create_new = create_new;
self
}
pub fn noatime(&mut self, noatime: bool) -> &mut Self {
self.noatime = noatime;
self
}
pub fn mode(&mut self, mode: u32) -> &mut Self {
self.mode = mode;
self
}
pub async fn open(&self, path: impl AsRef<Path>) -> io::Result<File> {
if crate::reactor::is_uring() {
let mut opts = uring::OpenOptions::new();
opts.read(self.read)
.write(self.write)
.append(self.append)
.truncate(self.truncate)
.create(self.create)
.create_new(self.create_new)
.noatime(self.noatime)
.mode(self.mode);
opts.open(path).await.map(File::Uring)
} else {
let mut opts = poll::OpenOptions::new();
opts.read(self.read)
.write(self.write)
.append(self.append)
.truncate(self.truncate)
.create(self.create)
.create_new(self.create_new)
.noatime(self.noatime)
.mode(self.mode);
opts.open(path).await.map(File::Poll)
}
}
}