Skip to main content

FrozenFile

Struct FrozenFile 

Source
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 mut data = vec![1u8; 0x10];
assert!(file.pwrite(data.as_mut_ptr(), 0).is_ok());
assert!(file.sync().is_ok());

let mut buf = vec![0u8; data.len()];
assert!(file.pread(buf.as_mut_ptr(), 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

Source

pub fn cfg(&self) -> &FFCfg

Fetch config used for FrozenFile

Source

pub fn length(&self) -> FrozenRes<usize>

Read current length of FrozenFile

Source

pub fn fd(&self) -> i32

Get file descriptor for FrozenFile

Source

pub fn exists(&self) -> FrozenRes<bool>

Check if FrozenFile exists on the fs

Source

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, FFileErr::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);
Source

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));
Source

pub fn sync(&self) -> FrozenRes<()>

Syncs in-mem data on the storage device

Source

pub fn sync_range(&self, index: usize, count: usize) -> FrozenRes<()>

Initiates writeback (best-effort) of dirty pages in the specified range

Source

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());
Source

pub fn pread(&self, buf: *mut u8, index: usize) -> FrozenRes<()>

Read a single chunk at given index w/ pread syscall

Source

pub fn pwrite(&self, buf: *mut u8, index: usize) -> FrozenRes<()>

Write a single chunk at given index w/ pwrite syscall

Source

pub fn preadv(&self, bufs: &[*mut u8], index: usize) -> FrozenRes<()>

Read multiple chunks starting from given index till bufs.len() w/ preadv syscall

Source

pub fn pwritev(&self, bufs: &[*mut u8], index: usize) -> FrozenRes<()>

Write multiple chunks starting from given index till bufs.len() w/ pwritev syscall

Trait Implementations§

Source§

impl Debug for FrozenFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for FrozenFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for FrozenFile

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for FrozenFile

Source§

impl Sync for FrozenFile

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.