#![doc(html_root_url = "https://docs.rs/fs-err/2.4.0")]
#![deny(missing_debug_implementations, missing_docs)]
mod dir;
mod errors;
mod file;
mod open_options;
mod path;
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> + Into<PathBuf>>(path: P) -> io::Result<Vec<u8>> {
let mut file = File::open(path)?;
let mut bytes = Vec::with_capacity(initial_buffer_size(&file));
file.read_to_end(&mut bytes)?;
Ok(bytes)
}
pub fn read_to_string<P: AsRef<Path> + Into<PathBuf>>(path: P) -> io::Result<String> {
let mut file = File::open(path)?;
let mut string = String::with_capacity(initial_buffer_size(&file));
file.read_to_string(&mut string)?;
Ok(string)
}
pub fn write<P: AsRef<Path> + Into<PathBuf>, C: AsRef<[u8]>>(
path: P,
contents: C,
) -> io::Result<()> {
File::create(path)?.write_all(contents.as_ref())
}
pub fn copy<P, Q>(from: P, to: Q) -> io::Result<u64>
where
P: AsRef<Path> + Into<PathBuf>,
Q: AsRef<Path> + Into<PathBuf>,
{
fs::copy(from.as_ref(), to.as_ref())
.map_err(|source| SourceDestError::new(source, SourceDestErrorKind::Copy, from, to))
}
pub fn create_dir<P>(path: P) -> io::Result<()>
where
P: AsRef<Path> + Into<PathBuf>,
{
fs::create_dir(path.as_ref()).map_err(|source| Error::new(source, ErrorKind::CreateDir, path))
}
pub fn create_dir_all<P>(path: P) -> io::Result<()>
where
P: AsRef<Path> + Into<PathBuf>,
{
fs::create_dir_all(path.as_ref())
.map_err(|source| Error::new(source, ErrorKind::CreateDir, path))
}
pub fn remove_dir<P>(path: P) -> io::Result<()>
where
P: AsRef<Path> + Into<PathBuf>,
{
fs::remove_dir(path.as_ref()).map_err(|source| Error::new(source, ErrorKind::RemoveDir, path))
}
pub fn remove_dir_all<P>(path: P) -> io::Result<()>
where
P: AsRef<Path> + Into<PathBuf>,
{
fs::remove_dir_all(path.as_ref())
.map_err(|source| Error::new(source, ErrorKind::RemoveDir, path))
}
pub fn remove_file<P>(path: P) -> io::Result<()>
where
P: AsRef<Path> + Into<PathBuf>,
{
fs::remove_file(path.as_ref()).map_err(|source| Error::new(source, ErrorKind::RemoveFile, path))
}
pub fn metadata<P: AsRef<Path> + Into<PathBuf>>(path: P) -> io::Result<fs::Metadata> {
fs::metadata(path.as_ref()).map_err(|source| Error::new(source, ErrorKind::Metadata, path))
}
pub fn canonicalize<P: AsRef<Path> + Into<PathBuf>>(path: P) -> io::Result<PathBuf> {
fs::canonicalize(path.as_ref())
.map_err(|source| Error::new(source, ErrorKind::Canonicalize, path))
}
pub fn hard_link<P: AsRef<Path> + Into<PathBuf>, Q: AsRef<Path> + Into<PathBuf>>(
src: P,
dst: Q,
) -> io::Result<()> {
fs::hard_link(src.as_ref(), dst.as_ref())
.map_err(|source| SourceDestError::new(source, SourceDestErrorKind::HardLink, src, dst))
}
pub fn read_link<P: AsRef<Path> + Into<PathBuf>>(path: P) -> io::Result<PathBuf> {
fs::read_link(path.as_ref()).map_err(|source| Error::new(source, ErrorKind::ReadLink, path))
}
pub fn rename<P: AsRef<Path> + Into<PathBuf>, Q: AsRef<Path> + Into<PathBuf>>(
from: P,
to: Q,
) -> io::Result<()> {
fs::rename(from.as_ref(), to.as_ref())
.map_err(|source| SourceDestError::new(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> + Into<PathBuf>, Q: AsRef<Path> + Into<PathBuf>>(
src: P,
dst: Q,
) -> io::Result<()> {
#[allow(deprecated)]
fs::soft_link(src.as_ref(), dst.as_ref())
.map_err(|source| SourceDestError::new(source, SourceDestErrorKind::SoftLink, src, dst))
}
pub fn symlink_metadata<P: AsRef<Path> + Into<PathBuf>>(path: P) -> io::Result<fs::Metadata> {
fs::symlink_metadata(path.as_ref())
.map_err(|source| Error::new(source, ErrorKind::SymlinkMetadata, path))
}
pub fn set_permissions<P: AsRef<Path> + Into<PathBuf>>(
path: P,
perm: fs::Permissions,
) -> io::Result<()> {
fs::set_permissions(path.as_ref(), perm)
.map_err(|source| Error::new(source, ErrorKind::SetPermissions, path))
}
fn initial_buffer_size(file: &File) -> usize {
file.file()
.metadata()
.map(|m| m.len() as usize + 1)
.unwrap_or(0)
}