use std::{
fs::{File, OpenOptions},
io,
path::PathBuf,
};
#[derive(Debug)]
pub struct FileInfo {
pub file: File,
pub path: PathBuf,
pub size: u64,
}
impl FileInfo {
pub fn new_from_path(path: impl Into<PathBuf>) -> io::Result<Self> {
let path = path.into();
let file = File::open(&path)?;
Self::new_from_path_and_file(path, file)
}
pub fn new_from_path_writable(path: impl Into<PathBuf>, is_writable: bool) -> io::Result<Self> {
let path = path.into();
let file = OpenOptions::new()
.read(true)
.create(false)
.write(is_writable)
.open(&path)?;
Self::new_from_path_and_file(path, file)
}
pub fn new_from_path_and_file(path: impl Into<PathBuf>, file: File) -> io::Result<Self> {
let size = file.metadata()?.len();
Ok(Self {
path: path.into(),
size,
file,
})
}
}