#![doc(html_root_url = "https://docs.rs/fs-err/2.10.0")]
#![deny(missing_debug_implementations, missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]
mod dir;
mod errors;
mod file;
mod open_options;
pub mod os;
mod path;
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub mod tokio;
use std::fs;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use errors::{Error, ErrorKind, SourceDestError, SourceDestErrorKind};
pub use dir::*;
pub use file::*;
pub use open_options::OpenOptions;
pub use path::PathExt;
pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
let path = path.as_ref();
let mut file = file::open(path).map_err(|err_gen| err_gen(path.to_path_buf()))?;
let mut bytes = Vec::with_capacity(initial_buffer_size(&file));
file.read_to_end(&mut bytes)
.map_err(|err| Error::build(err, ErrorKind::Read, path))?;
Ok(bytes)
}
pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
let path = path.as_ref();
let mut file = file::open(path).map_err(|err_gen| err_gen(path.to_path_buf()))?;
let mut string = String::with_capacity(initial_buffer_size(&file));
file.read_to_string(&mut string)
.map_err(|err| Error::build(err, ErrorKind::Read, path))?;
Ok(string)
}
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
let path = path.as_ref();
file::create(path)
.map_err(|err_gen| err_gen(path.to_path_buf()))?
.write_all(contents.as_ref())
.map_err(|err| Error::build(err, ErrorKind::Write, path))
}
pub fn copy<P, Q>(from: P, to: Q) -> io::Result<u64>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
let from = from.as_ref();
let to = to.as_ref();
fs::copy(from, to)
.map_err(|source| SourceDestError::build(source, SourceDestErrorKind::Copy, from, to))
}
pub fn create_dir<P>(path: P) -> io::Result<()>
where
P: AsRef<Path>,
{
let path = path.as_ref();
fs::create_dir(path).map_err(|source| Error::build(source, ErrorKind::CreateDir, path))
}
pub fn create_dir_all<P>(path: P) -> io::Result<()>
where
P: AsRef<Path>,
{
let path = path.as_ref();
fs::create_dir_all(path).map_err(|source| Error::build(source, ErrorKind::CreateDir, path))
}
pub fn remove_dir<P>(path: P) -> io::Result<()>
where
P: AsRef<Path>,
{
let path = path.as_ref();
fs::remove_dir(path).map_err(|source| Error::build(source, ErrorKind::RemoveDir, path))
}
pub fn remove_dir_all<P>(path: P) -> io::Result<()>
where
P: AsRef<Path>,
{
let path = path.as_ref();
fs::remove_dir_all(path).map_err(|source| Error::build(source, ErrorKind::RemoveDir, path))
}
pub fn remove_file<P>(path: P) -> io::Result<()>
where
P: AsRef<Path>,
{
let path = path.as_ref();
fs::remove_file(path).map_err(|source| Error::build(source, ErrorKind::RemoveFile, path))
}
pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<fs::Metadata> {
let path = path.as_ref();
fs::metadata(path).map_err(|source| Error::build(source, ErrorKind::Metadata, path))
}
pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
let path = path.as_ref();
fs::canonicalize(path).map_err(|source| Error::build(source, ErrorKind::Canonicalize, path))
}
pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
let src = src.as_ref();
let dst = dst.as_ref();
fs::hard_link(src, dst)
.map_err(|source| SourceDestError::build(source, SourceDestErrorKind::HardLink, src, dst))
}
pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
let path = path.as_ref();
fs::read_link(path).map_err(|source| Error::build(source, ErrorKind::ReadLink, path))
}
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
let from = from.as_ref();
let to = to.as_ref();
fs::rename(from, to)
.map_err(|source| SourceDestError::build(source, SourceDestErrorKind::Rename, from, to))
}
#[deprecated = "replaced with std::os::unix::fs::symlink and \
std::os::windows::fs::{symlink_file, symlink_dir}"]
pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
let src = src.as_ref();
let dst = dst.as_ref();
#[allow(deprecated)]
fs::soft_link(src, dst)
.map_err(|source| SourceDestError::build(source, SourceDestErrorKind::SoftLink, src, dst))
}
pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<fs::Metadata> {
let path = path.as_ref();
fs::symlink_metadata(path)
.map_err(|source| Error::build(source, ErrorKind::SymlinkMetadata, path))
}
pub fn set_permissions<P: AsRef<Path>>(path: P, perm: fs::Permissions) -> io::Result<()> {
let path = path.as_ref();
fs::set_permissions(path, perm)
.map_err(|source| Error::build(source, ErrorKind::SetPermissions, path))
}
fn initial_buffer_size(file: &std::fs::File) -> usize {
file.metadata().map(|m| m.len() as usize + 1).unwrap_or(0)
}
pub(crate) use private::Sealed;
mod private {
pub trait Sealed {}
impl Sealed for crate::File {}
impl Sealed for std::path::Path {}
impl Sealed for crate::OpenOptions {}
}