use crate::error::{Error, ErrorKind, Result};
#[cfg(feature = "nightly")]
use std::io;
use std::path::Path;
cfg_sync! {
use std::fs::{File, OpenOptions};
pub fn sync_dir<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref();
if !path.is_dir() {
#[cfg(feature = "nightly")]
return Err(Error::new(ErrorKind::IO, io::Error::from(io::ErrorKind::NotADirectory)));
#[cfg(not(feature = "nightly"))]
return Err(Error::from(ErrorKind::NotADirectory));
}
File::open(path)
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))?
.sync_all()
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))
}
pub fn sync_parent<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref().canonicalize().map_err(|e| Error::new(ErrorKind::IO, e))?;
let path = path.parent().unwrap();
if !path.is_dir() {
#[cfg(feature = "nightly")]
return Err(Error::new(ErrorKind::IO, io::Error::from(io::ErrorKind::NotADirectory)));
#[cfg(not(feature = "nightly"))]
return Err(Error::from(ErrorKind::NotADirectory));
}
File::open(path)
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))?
.sync_all()
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))
}
pub fn open_read_only_file<P: AsRef<Path>>(path: P) -> Result<File> {
OpenOptions::new()
.read(true)
.open(path)
.map_err(|e| Error::new(ErrorKind::IO, e))
}
pub fn open_exist_file<P: AsRef<Path>>(path: P) -> Result<File> {
OpenOptions::new()
.read(true)
.write(true)
.append(false)
.open(path)
.map_err(|e| Error::new(ErrorKind::IO, e))
}
pub fn open_exist_file_with_append<P: AsRef<Path>>(path: P) -> Result<File> {
OpenOptions::new()
.read(true)
.write(true)
.append(true)
.open(path)
.map_err(|e| Error::new(ErrorKind::IO, e))
}
pub fn open_file_with_truncate<P: AsRef<Path>>(path: P) -> Result<File> {
OpenOptions::new()
.read(true)
.write(true)
.truncate(true)
.open(path)
.map_err(|e| Error::new(ErrorKind::IO, e))
}
pub fn open_or_create_file<P: AsRef<Path>>(path: P) -> Result<File> {
OpenOptions::new()
.create(true)
.read(true)
.write(true)
.open(path)
.map_err(|e| Error::new(ErrorKind::IO, e))
}
pub fn create_file<P: AsRef<Path>>(path: P) -> Result<File> {
OpenOptions::new()
.create_new(true)
.read(true)
.write(true)
.append(true)
.open(path)
.map_err(|e| Error::new(ErrorKind::IO, e))
}
}
cfg_async! {
macro_rules! impl_async_file_utils {
($file: ident, $open_options: ident) => {
pub async fn open_read_only_file_async<P: AsRef<Path>>(path: P) -> Result<$file> {
<$open_options>::new()
.read(true)
.open(path)
.await
.map_err(|e| Error::new(ErrorKind::IO, e))
}
pub async fn open_exist_file_async<P: AsRef<Path>>(path: P) -> Result<$file> {
<$open_options>::new()
.read(true)
.write(true)
.append(false)
.open(path)
.await
.map_err(|e| Error::new(ErrorKind::IO, e))
}
pub async fn open_exist_file_with_append_async<P: AsRef<Path>>(path: P) -> Result<$file> {
<$open_options>::new()
.read(true)
.write(true)
.append(true)
.open(path)
.await
.map_err(|e| Error::new(ErrorKind::IO, e))
}
pub async fn open_file_with_truncate_async<P: AsRef<Path>>(path: P) -> Result<$file> {
<$open_options>::new()
.read(true)
.write(true)
.truncate(true)
.open(path)
.await
.map_err(|e| Error::new(ErrorKind::IO, e))
}
pub async fn open_or_create_file_async<P: AsRef<Path>>(path: P) -> Result<$file> {
<$open_options>::new()
.create(true)
.read(true)
.write(true)
.open(path)
.await
.map_err(|e| Error::new(ErrorKind::IO, e))
}
pub async fn create_file_async<P: AsRef<Path>>(path: P) -> Result<$file> {
<$open_options>::new()
.create_new(true)
.read(true)
.write(true)
.append(true)
.open(path)
.await
.map_err(|e| Error::new(ErrorKind::IO, e))
}
};
}
}
cfg_smol! {
pub mod smol {
use smol::fs::{File, OpenOptions};
use crate::error::{Error, ErrorKind, Result};
#[cfg(feature = "nightly")]
use std::io;
use std::path::Path;
pub async fn sync_dir_async<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref();
if !path.is_dir() {
#[cfg(feature = "nightly")]
return Err(Error::new(ErrorKind::IO, io::Error::from(io::ErrorKind::NotADirectory)));
#[cfg(not(feature = "nightly"))]
return Err(Error::from(ErrorKind::NotADirectory));
}
File::open(path)
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))?
.sync_all()
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))
}
pub async fn sync_parent_async<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref().canonicalize().map_err(|e| Error::new(ErrorKind::IO, e))?;
let path = path.parent().unwrap();
if !path.is_dir() {
#[cfg(feature = "nightly")]
return Err(Error::new(ErrorKind::IO, io::Error::from(io::ErrorKind::NotADirectory)));
#[cfg(not(feature = "nightly"))]
return Err(Error::from(ErrorKind::NotADirectory));
}
File::open(path)
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))?
.sync_all()
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))
}
impl_async_file_utils!(File, OpenOptions);
}
}
cfg_tokio! {
pub mod tokio {
use tokio::fs::{File, OpenOptions};
use crate::error::{Error, ErrorKind, Result};
#[cfg(feature = "nightly")]
use std::io;
use std::path::Path;
pub async fn sync_dir_async<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref();
if !path.is_dir() {
#[cfg(feature = "nightly")]
return Err(Error::new(ErrorKind::IO, io::Error::from(io::ErrorKind::NotADirectory)));
#[cfg(not(feature = "nightly"))]
return Err(Error::from(ErrorKind::NotADirectory));
}
File::open(path)
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))?
.sync_all()
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))
}
pub async fn sync_parent_async<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref().canonicalize().map_err(|e| Error::new(ErrorKind::IO, e))?;
let path = path.parent().unwrap();
if !path.is_dir() {
#[cfg(feature = "nightly")]
return Err(Error::new(ErrorKind::IO, io::Error::from(io::ErrorKind::NotADirectory)));
#[cfg(not(feature = "nightly"))]
return Err(Error::from(ErrorKind::NotADirectory));
}
File::open(path)
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))?
.sync_all()
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))
}
impl_async_file_utils!(File, OpenOptions);
}
}
cfg_async_std! {
pub mod async_std {
use async_std::fs::{File, OpenOptions};
use crate::error::{Error, ErrorKind, Result};
#[cfg(feature = "nightly")]
use std::io;
use async_std::path::Path;
pub async fn sync_dir_async<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref();
if !path.is_dir().await {
#[cfg(feature = "nightly")]
return Err(Error::new(ErrorKind::IO, io::Error::from(io::ErrorKind::NotADirectory)));
#[cfg(not(feature = "nightly"))]
return Err(Error::from(ErrorKind::NotADirectory));
}
File::open(path)
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))?
.sync_all()
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))
}
pub async fn sync_parent_async<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref().canonicalize().await.map_err(|e| Error::new(ErrorKind::IO, e))?;
let path = path.parent().unwrap();
if !path.is_dir().await {
#[cfg(feature = "nightly")]
return Err(Error::new(ErrorKind::IO, io::Error::from(io::ErrorKind::NotADirectory)));
#[cfg(not(feature = "nightly"))]
return Err(Error::from(ErrorKind::NotADirectory));
}
File::open(path)
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))?
.sync_all()
.await
.map_err(|e| Error::new_source_msg(ErrorKind::OpenFailed, path.to_string_lossy(), e))
}
impl_async_file_utils!(File, OpenOptions);
}
}