#[doc(inline)]
pub use async_fs::*;
use std::fmt;
use std::future::Future;
use std::io::{self, SeekFrom};
use std::path::Path;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt as _;
#[cfg(windows)]
use std::os::windows::fs::OpenOptionsExt as _;
use async_lock::Mutex;
use blocking::{Unblock, unblock};
use futures_lite::io::{AsyncRead, AsyncSeek, AsyncWrite, AsyncWriteExt};
use futures_lite::ready;
#[doc(no_inline)]
pub use std::fs::{FileType, Metadata, Permissions};
pub struct File {
file: Arc<std::fs::File>,
unblock: Mutex<Unblock<ArcFile>>,
read_pos: Option<io::Result<u64>>,
is_dirty: bool,
}
impl File {
fn new(inner: std::fs::File, is_dirty: bool) -> File {
let file = Arc::new(inner);
let unblock = Mutex::new(Unblock::new(ArcFile(file.clone())));
let read_pos = None;
File {
file,
unblock,
read_pos,
is_dirty,
}
}
pub async fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
let path = path.as_ref().to_owned();
let file = unblock(move || std::fs::File::open(path)).await?;
Ok(File::new(file, false))
}
pub async fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
let path = path.as_ref().to_owned();
let file = unblock(move || std::fs::File::create(path)).await?;
Ok(File::new(file, false))
}
pub async fn sync_all(&self) -> io::Result<()> {
let mut inner = self.unblock.lock().await;
inner.flush().await?;
let file = self.file.clone();
unblock(move || file.sync_all()).await
}
pub async fn sync_data(&self) -> io::Result<()> {
let mut inner = self.unblock.lock().await;
inner.flush().await?;
let file = self.file.clone();
unblock(move || file.sync_data()).await
}
pub async fn set_len(&self, size: u64) -> io::Result<()> {
let mut inner = self.unblock.lock().await;
inner.flush().await?;
let file = self.file.clone();
unblock(move || file.set_len(size)).await
}
pub async fn metadata(&self) -> io::Result<Metadata> {
let file = self.file.clone();
unblock(move || file.metadata()).await
}
pub async fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
let file = self.file.clone();
unblock(move || file.set_permissions(perm)).await
}
fn poll_reposition(&mut self, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
if let Some(Ok(read_pos)) = self.read_pos {
ready!(Pin::new(self.unblock.get_mut()).poll_seek(cx, SeekFrom::Start(read_pos)))?;
}
self.read_pos = None;
Poll::Ready(Ok(()))
}
pub async fn try_unwrap(self) -> Result<std::fs::File, Self> {
let _ = self.unblock.into_inner().into_inner().await;
match Arc::try_unwrap(self.file) {
Ok(ready) => Ok(ready),
Err(pending) => {
Err(Self {
file: pending.clone(),
unblock: Mutex::new(Unblock::new(ArcFile(pending))),
is_dirty: false,
read_pos: self.read_pos,
})
}
}
}
}
impl fmt::Debug for File {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.file.fmt(f)
}
}
impl From<std::fs::File> for File {
fn from(inner: std::fs::File) -> File {
File::new(inner, true)
}
}
#[cfg(unix)]
impl std::os::unix::io::AsRawFd for File {
fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
self.file.as_raw_fd()
}
}
#[cfg(windows)]
impl std::os::windows::io::AsRawHandle for File {
fn as_raw_handle(&self) -> std::os::windows::io::RawHandle {
self.file.as_raw_handle()
}
}
#[cfg(unix)]
impl From<std::os::unix::io::OwnedFd> for File {
fn from(fd: std::os::unix::io::OwnedFd) -> Self {
File::from(std::fs::File::from(fd))
}
}
#[cfg(windows)]
impl From<std::os::windows::io::OwnedHandle> for File {
fn from(fd: std::os::windows::io::OwnedHandle) -> Self {
File::from(std::fs::File::from(fd))
}
}
#[cfg(unix)]
impl std::os::unix::io::AsFd for File {
fn as_fd(&self) -> std::os::unix::io::BorrowedFd<'_> {
self.file.as_fd()
}
}
#[cfg(windows)]
impl std::os::windows::io::AsHandle for File {
fn as_handle(&self) -> std::os::windows::io::BorrowedHandle<'_> {
self.file.as_handle()
}
}
impl AsyncRead for File {
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
if self.read_pos.is_none() {
self.read_pos = Some(ready!(self.as_mut().poll_seek(cx, SeekFrom::Current(0))));
}
let n = ready!(Pin::new(self.unblock.get_mut()).poll_read(cx, buf))?;
if let Some(Ok(pos)) = self.read_pos.as_mut() {
*pos += n as u64;
}
Poll::Ready(Ok(n))
}
}
impl AsyncWrite for File {
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
ready!(self.poll_reposition(cx))?;
self.is_dirty = true;
Pin::new(self.unblock.get_mut()).poll_write(cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
if self.is_dirty {
ready!(Pin::new(self.unblock.get_mut()).poll_flush(cx))?;
self.is_dirty = false;
}
Poll::Ready(Ok(()))
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(self.unblock.get_mut()).poll_close(cx)
}
}
impl AsyncSeek for File {
fn poll_seek(mut self: Pin<&mut Self>, cx: &mut Context<'_>, pos: SeekFrom) -> Poll<io::Result<u64>> {
ready!(self.poll_reposition(cx))?;
Pin::new(self.unblock.get_mut()).poll_seek(cx, pos)
}
}
struct ArcFile(Arc<std::fs::File>);
impl io::Read for ArcFile {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
(&*self.0).read(buf)
}
}
impl io::Write for ArcFile {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
(&*self.0).write(buf)
}
fn flush(&mut self) -> io::Result<()> {
(&*self.0).flush()
}
}
impl io::Seek for ArcFile {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
(&*self.0).seek(pos)
}
}
#[derive(Clone, Debug)]
pub struct OpenOptions(std::fs::OpenOptions);
impl OpenOptions {
pub fn new() -> OpenOptions {
OpenOptions(std::fs::OpenOptions::new())
}
pub fn read(&mut self, read: bool) -> &mut OpenOptions {
self.0.read(read);
self
}
pub fn write(&mut self, write: bool) -> &mut OpenOptions {
self.0.write(write);
self
}
pub fn append(&mut self, append: bool) -> &mut OpenOptions {
self.0.append(append);
self
}
pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
self.0.truncate(truncate);
self
}
pub fn create(&mut self, create: bool) -> &mut OpenOptions {
self.0.create(create);
self
}
pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
self.0.create_new(create_new);
self
}
pub fn open<P: AsRef<Path>>(&self, path: P) -> impl Future<Output = io::Result<File>> {
let path = path.as_ref().to_owned();
let options = self.0.clone();
async move {
let file = unblock(move || options.open(path)).await?;
Ok(File::new(file, false))
}
}
}
impl Default for OpenOptions {
fn default() -> Self {
Self::new()
}
}
#[cfg(unix)]
impl unix::OpenOptionsExt for OpenOptions {
fn mode(&mut self, mode: u32) -> &mut Self {
self.0.mode(mode);
self
}
fn custom_flags(&mut self, flags: i32) -> &mut Self {
self.0.custom_flags(flags);
self
}
}
#[cfg(windows)]
impl windows::OpenOptionsExt for OpenOptions {
fn access_mode(&mut self, access: u32) -> &mut Self {
self.0.access_mode(access);
self
}
fn share_mode(&mut self, val: u32) -> &mut Self {
self.0.share_mode(val);
self
}
fn custom_flags(&mut self, flags: u32) -> &mut Self {
self.0.custom_flags(flags);
self
}
fn attributes(&mut self, val: u32) -> &mut Self {
self.0.attributes(val);
self
}
fn security_qos_flags(&mut self, flags: u32) -> &mut Self {
self.0.security_qos_flags(flags);
self
}
}
#[cfg_attr(not(any(windows, unix)), allow(dead_code))]
mod __private {
#[doc(hidden)]
pub trait Sealed {}
impl Sealed for super::OpenOptions {}
impl Sealed for super::File {}
}
#[cfg(unix)]
pub mod unix {
use super::__private::Sealed;
#[doc(inline)]
pub use async_fs::unix::*;
pub trait OpenOptionsExt: Sealed {
fn mode(&mut self, mode: u32) -> &mut Self;
fn custom_flags(&mut self, flags: i32) -> &mut Self;
}
}
#[cfg(windows)]
pub mod windows {
use super::__private::Sealed;
#[doc(inline)]
pub use async_fs::windows::*;
pub trait OpenOptionsExt: Sealed {
fn access_mode(&mut self, access: u32) -> &mut Self;
fn share_mode(&mut self, val: u32) -> &mut Self;
fn custom_flags(&mut self, flags: u32) -> &mut Self;
fn attributes(&mut self, val: u32) -> &mut Self;
fn security_qos_flags(&mut self, flags: u32) -> &mut Self;
}
}