use std::{io, path::Path};
use compio_buf::{BufResult, IoBuf, buf_try};
use compio_io::{AsyncReadAtExt, AsyncWriteAtExt};
use crate::{DirBuilder, File, Metadata, OpenOptions};
#[cfg(unix)]
#[path = "unix.rs"]
mod sys;
#[cfg(windows)]
#[path = "windows.rs"]
mod sys;
#[derive(Debug, Clone)]
pub struct Dir {
inner: sys::Dir,
}
impl Dir {
pub async fn open(path: impl AsRef<Path>) -> io::Result<Self> {
Ok(Dir {
inner: sys::Dir::open(path).await?,
})
}
pub async fn open_file_with(
&self,
path: impl AsRef<Path>,
options: &OpenOptions,
) -> io::Result<File> {
self.inner.open_file_with(path, options).await
}
pub async fn open_file(&self, path: impl AsRef<Path>) -> io::Result<File> {
self.open_file_with(path, OpenOptions::new().read(true))
.await
}
pub async fn create_file(&self, path: impl AsRef<Path>) -> io::Result<File> {
self.open_file_with(
path,
OpenOptions::new().write(true).create(true).truncate(true),
)
.await
}
pub async fn open_dir(&self, path: impl AsRef<Path>) -> io::Result<Self> {
Ok(Self {
inner: self.inner.open_dir(path).await?,
})
}
pub async fn create_dir_with(
&self,
path: impl AsRef<Path>,
builder: &DirBuilder,
) -> io::Result<()> {
self.inner.create_dir_with(path, builder).await
}
pub async fn create_dir(&self, path: impl AsRef<Path>) -> io::Result<()> {
self.create_dir_with(path, &DirBuilder::new()).await
}
pub async fn create_dir_all(&self, path: impl AsRef<Path>) -> io::Result<()> {
self.create_dir_with(path, DirBuilder::new().recursive(true))
.await
}
pub async fn dir_metadata(&self) -> io::Result<Metadata> {
self.inner.dir_metadata().await
}
pub async fn metadata(&self, path: impl AsRef<Path>) -> io::Result<Metadata> {
self.inner.metadata(path).await
}
pub async fn symlink_metadata(&self, path: impl AsRef<Path>) -> io::Result<Metadata> {
self.inner.symlink_metadata(path).await
}
pub async fn hard_link(
&self,
source: impl AsRef<Path>,
target_dir: &Self,
target: impl AsRef<Path>,
) -> io::Result<()> {
self.inner
.hard_link(source, &target_dir.inner, target)
.await
}
#[cfg(unix)]
pub async fn symlink(
&self,
original: impl AsRef<Path>,
link: impl AsRef<Path>,
) -> io::Result<()> {
self.inner.symlink(original, link).await
}
#[cfg(windows)]
pub async fn symlink_file(
&self,
original: impl AsRef<Path>,
link: impl AsRef<Path>,
) -> io::Result<()> {
self.inner.symlink_file(original, link).await
}
#[cfg(windows)]
pub async fn symlink_dir(
&self,
original: impl AsRef<Path>,
link: impl AsRef<Path>,
) -> io::Result<()> {
self.inner.symlink_dir(original, link).await
}
pub async fn rename(
&self,
from: impl AsRef<Path>,
to_dir: &Self,
to: impl AsRef<Path>,
) -> io::Result<()> {
self.inner.rename(from, &to_dir.inner, to).await
}
pub async fn remove_file(&self, path: impl AsRef<Path>) -> io::Result<()> {
self.inner.remove_file(path).await
}
pub async fn remove_dir(&self, path: impl AsRef<Path>) -> io::Result<()> {
self.inner.remove_dir(path).await
}
pub async fn read(&self, path: impl AsRef<Path>) -> io::Result<Vec<u8>> {
let file = self.open_file(path).await?;
let BufResult(res, buf) = file.read_to_end_at(Vec::new(), 0).await;
res?;
Ok(buf)
}
pub async fn write<B: IoBuf>(&self, path: impl AsRef<Path>, buf: B) -> BufResult<(), B> {
let (mut file, buf) = buf_try!(self.create_file(path).await, buf);
file.write_all_at(buf, 0).await
}
}
compio_driver::impl_raw_fd!(Dir, std::fs::File, inner);