pub struct FrozenFile { /* private fields */ }Expand description
Custom implementation of std::fs::File
§Example
use frozen_core::ffile::{FrozenFile, FFCfg};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tmp_frozen_file");
let cfg = FFCfg {
mid: 0x00,
chunk_size: 0x10,
path: path.to_path_buf(),
initial_chunk_amount: 0x0A,
};
let file = FrozenFile::new(cfg.clone()).unwrap();
assert_eq!(file.length().unwrap(), 0x10 * 0x0A);
let data = vec![1u8; 0x10];
let iov = libc::iovec {
iov_base: data.as_ptr() as *mut _,
iov_len: data.len(),
};
assert!(file.write(&iov, 0).is_ok());
assert!(file.sync().is_ok());
let mut buf = vec![0u8; data.len()];
let mut read_iov = libc::iovec {
iov_base: buf.as_mut_ptr() as *mut _,
iov_len: buf.len(),
};
assert!(file.read(&mut read_iov, 0).is_ok());
assert_eq!(buf, data);
assert!(FrozenFile::new(cfg.clone()).is_err());
assert!(file.delete().is_ok());
assert!(!path.exists());
drop(file);
assert!(FrozenFile::new(cfg).is_ok());Implementations§
Source§impl FrozenFile
impl FrozenFile
Sourcepub fn length(&self) -> FrozenRes<usize>
pub fn length(&self) -> FrozenRes<usize>
Read current length of FrozenFile
Sourcepub fn fd(&self) -> i32
pub fn fd(&self) -> i32
Get file descriptor for FrozenFile
Sourcepub fn exists(&self) -> FrozenRes<bool>
pub fn exists(&self) -> FrozenRes<bool>
Check if FrozenFile exists on the fs
Sourcepub fn new(cfg: FFCfg) -> FrozenRes<Self>
pub fn new(cfg: FFCfg) -> FrozenRes<Self>
Create a new or open an existing FrozenFile
§FFCfg
All configs for FrozenFile are stored in FFCfg
§Important
The cfg must not change any of its properties for the entire life of FrozenFile,
one must use config stores like Rta to store config
§Multiple Instances
We acquire an exclusive lock for the entire file, this protects against operating with
multiple simultenious instance of FrozenFile, when trying to call FrozenFile::new
when already called, FFileErrRes::Lck error will be thrown
§Example
use frozen_core::ffile::{FrozenFile, FFCfg};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tmp_frozen_file");
let cfg = FFCfg {
mid: 0x00,
chunk_size: 0x10,
path: path.to_path_buf(),
initial_chunk_amount: 0x0A,
};
let file = FrozenFile::new(cfg).unwrap();
assert_eq!(file.length().unwrap(), 0x10 * 0x0A);Sourcepub fn grow(&self, count: usize) -> FrozenRes<()>
pub fn grow(&self, count: usize) -> FrozenRes<()>
Grow file size of FrozenFile by given count of chunks
§Example
use frozen_core::ffile::{FrozenFile, FFCfg};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tmp_frozen_file");
let cfg = FFCfg {
mid: 0x00,
chunk_size: 0x10,
path: path.to_path_buf(),
initial_chunk_amount: 0x0A,
};
let file = FrozenFile::new(cfg).unwrap();
assert_eq!(file.length().unwrap(), 0x10 * 0x0A);
file.grow(0x20).unwrap();
assert_eq!(file.length().unwrap(), 0x10 * (0x0A + 0x20));Sourcepub fn sync_range(&self, index: usize, count: usize) -> FrozenRes<()>
pub fn sync_range(&self, index: usize, count: usize) -> FrozenRes<()>
Initiates writeback (best-effort) of dirty pages in the specified range
Sourcepub fn delete(&self) -> FrozenRes<()>
pub fn delete(&self) -> FrozenRes<()>
Delete FrozenFile from fs
§Example
use frozen_core::ffile::{FrozenFile, FFCfg};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tmp_frozen_file");
let cfg = FFCfg {
mid: 0x00,
chunk_size: 0x10,
path: path.to_path_buf(),
initial_chunk_amount: 0x0A,
};
let file = FrozenFile::new(cfg).unwrap();
assert!(file.exists().unwrap());
file.delete().unwrap();
assert!(!file.exists().unwrap());Sourcepub fn read(&self, buf: &mut iovec, index: usize) -> FrozenRes<()>
pub fn read(&self, buf: &mut iovec, index: usize) -> FrozenRes<()>
Read a single iovec chunk at given index w/ pread syscall
Sourcepub fn write(&self, buf: &iovec, index: usize) -> FrozenRes<()>
pub fn write(&self, buf: &iovec, index: usize) -> FrozenRes<()>
Write a single iovec chunk at given index w/ pwrite syscall