agave_fs/file_info.rs
1use std::{fs::File, io, path::PathBuf};
2
3/// Open `File` coupled with its filesystem location and most useful information
4///
5/// The attached context for the `File` is kept minimal to make it easy to construct
6/// without unnecessary kernel queries, but allowing users to:
7/// * associate the file received in callbacks to the request (by its path)
8/// * get the file's most useful metadata information
9#[derive(Debug)]
10pub struct FileInfo {
11 pub file: File,
12 pub path: PathBuf,
13 pub size: u64,
14}
15
16impl FileInfo {
17 /// Create new instance by opening a file from a given `path` and reading its metadata
18 pub fn new_from_path(path: impl Into<PathBuf>) -> io::Result<Self> {
19 let path = path.into();
20 let file = File::open(&path)?;
21 Self::new_from_path_and_file(path, file)
22 }
23
24 /// Create new instance by using already open `file` and only reading its metadata
25 pub fn new_from_path_and_file(path: impl Into<PathBuf>, file: File) -> io::Result<Self> {
26 let size = file.metadata()?.len();
27 Ok(Self {
28 path: path.into(),
29 size,
30 file,
31 })
32 }
33}