use std::{
fs,
io::{self, BufWriter, Read},
path::Path,
};
pub fn try_read<P: AsRef<Path>>(path: P) -> ForemanResult<Option<Vec<u8>>> {
let path = path.as_ref();
match fs::read(&path).map(Some) {
Ok(contents) => Ok(contents),
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
Ok(None)
} else {
Err(ForemanError::read_error(err, path))
}
}
}
}
pub fn try_read_to_string<P: AsRef<Path>>(path: P) -> ForemanResult<Option<String>> {
let path = path.as_ref();
match fs::read_to_string(&path).map(Some) {
Ok(contents) => Ok(contents),
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
Ok(None)
} else {
Err(ForemanError::read_error(err, path))
}
}
}
}
pub fn write_if_not_found<P: AsRef<Path>, C: AsRef<[u8]>>(
path: P,
contents: C,
) -> ForemanResult<()> {
let path = path.as_ref();
if let Err(err) = std::fs::metadata(&path) {
if err.kind() == io::ErrorKind::NotFound {
write(&path, contents)
} else {
Err(ForemanError::write_error(err, path))
}
} else {
Ok(())
}
}
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> ForemanResult<()> {
let path = path.as_ref();
fs::write(path, contents).map_err(|source| ForemanError::write_error(source, path))
}
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(source_path: P, dest_path: Q) -> ForemanResult<u64> {
let source_path = source_path.as_ref();
let dest_path = dest_path.as_ref();
fs::copy(source_path, dest_path)
.map_err(|source| ForemanError::copy_error(source, source_path, dest_path))
}
pub fn copy_from_reader<R: Read + ?Sized, P: AsRef<Path>>(
reader: &mut R,
dest_path: P,
) -> ForemanResult<u64> {
let dest_path = dest_path.as_ref();
let output_file = std::fs::File::create(&dest_path)
.map_err(|err| ForemanError::create_file_error(err, &dest_path))?;
let mut output = BufWriter::new(output_file);
io::copy(reader, &mut output).map_err(|err| ForemanError::write_error(err, &dest_path))
}
pub fn create_dir_all<P: AsRef<Path>>(path: P) -> ForemanResult<()> {
let path = path.as_ref();
fs::create_dir_all(path).map_err(|source| ForemanError::write_error(source, path))
}
pub use fs::Permissions;
use crate::error::{ForemanError, ForemanResult};
#[cfg(unix)]
pub fn set_permissions<P: AsRef<Path>>(path: P, permissions: Permissions) -> ForemanResult<()> {
let path = path.as_ref();
fs::set_permissions(path, permissions)
.map_err(|source| ForemanError::set_permission_error(source, path))
}