Something went wrong. Try again.
Rust client for LazyFS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396use std::fmt;use std::path::Path;
#[derive(Debug, Clone, PartialEq, Eq)]pub enum LazyFsCommand { ClearCache, CacheCheckpoint, DisplayCacheUsage, UnsyncedDataReport, Help, Crash(CrashCommand), TornOp(TornOpCommand), TornSeq(TornSeqCommand),}
impl LazyFsCommand { pub fn render(&self) -> String { match self { Self::ClearCache => "lazyfs::clear-cache".to_string(), Self::CacheCheckpoint => "lazyfs::cache-checkpoint".to_string(), Self::DisplayCacheUsage => "lazyfs::display-cache-usage".to_string(), Self::UnsyncedDataReport => "lazyfs::unsynced-data-report".to_string(), Self::Help => "lazyfs::help".to_string(), Self::Crash(command) => command.render(), Self::TornOp(command) => command.render(), Self::TornSeq(command) => command.render(), } }}
#[derive(Debug, Clone, PartialEq, Eq)]pub enum LazyFsCommandError { MissingCrashPathRegex, EmptyPersistSet { command: &'static str, }, ZeroPersistIndex { command: &'static str, }, PersistIndexOutOfRange { command: &'static str, index: usize, max: usize, }, InvalidCommandArgument { command: &'static str, argument: &'static str, reason: &'static str, }, ZeroTornOpParts, EmptyTornOpPartBytes, ZeroTornOpPartBytes,}
impl fmt::Display for LazyFsCommandError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::MissingCrashPathRegex => { f.write_str("multi-path crash commands need at least one path regex") } Self::EmptyPersistSet { command } => { write!(f, "{command} commands need at least one persist index") } Self::ZeroPersistIndex { command } => { write!( f, "{command} persist indexes are 1-based and cannot be zero" ) } Self::PersistIndexOutOfRange { command, index, max, } => write!( f, "{command} persist index {index} exceeds the maximum part index {max}" ), Self::InvalidCommandArgument { command, argument, reason, } => write!(f, "{command} argument {argument} is invalid: {reason}"), Self::ZeroTornOpParts => f.write_str("torn-op parts must be greater than zero"), Self::EmptyTornOpPartBytes => { f.write_str("torn-op parts_bytes must contain at least one part") } Self::ZeroTornOpPartBytes => { f.write_str("torn-op parts_bytes values must be greater than zero") } } }}
impl std::error::Error for LazyFsCommandError {}
fn validate_persist(command: &'static str, persist: &[usize]) -> Result<(), LazyFsCommandError> { if persist.is_empty() { return Err(LazyFsCommandError::EmptyPersistSet { command }); } if persist.contains(&0) { return Err(LazyFsCommandError::ZeroPersistIndex { command }); } Ok(())}
fn validate_command_argument( command: &'static str, argument: &'static str, value: &str,) -> Result<(), LazyFsCommandError> { let reason = if value.is_empty() { Some("empty values are not accepted by LazyFS") } else if value == "none" { Some("'none' is reserved as LazyFS's missing-value sentinel") } else if value.contains("::") { Some("contains LazyFS command delimiter '::'") } else if value.contains('\0') { Some("contains NUL") } else if value.contains('\n') || value.contains('\r') { Some("contains a line break") } else { None };
if let Some(reason) = reason { return Err(LazyFsCommandError::InvalidCommandArgument { command, argument, reason, }); }
Ok(())}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum Timing { Before, After,}
impl fmt::Display for Timing { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Before => f.write_str("before"), Self::After => f.write_str("after"), } }}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum CrashOp { Unlink, Truncate, Fsync, Write, Create, Access, Open, Read, Rename, Link, Symlink,}
impl CrashOp { fn is_multi_path(self) -> bool { matches!(self, Self::Rename | Self::Link | Self::Symlink) }}
impl fmt::Display for CrashOp { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Unlink => f.write_str("unlink"), Self::Truncate => f.write_str("truncate"), Self::Fsync => f.write_str("fsync"), Self::Write => f.write_str("write"), Self::Create => f.write_str("create"), Self::Access => f.write_str("access"), Self::Open => f.write_str("open"), Self::Read => f.write_str("read"), Self::Rename => f.write_str("rename"), Self::Link => f.write_str("link"), Self::Symlink => f.write_str("symlink"), } }}
#[derive(Debug, Clone, PartialEq, Eq)]pub struct CrashCommand { timing: Timing, op: CrashOp, from_rgx: Option<String>, to_rgx: Option<String>,}
impl CrashCommand { pub fn single_path( timing: Timing, op: CrashOp, from_rgx: impl Into<String>, ) -> Result<Self, LazyFsCommandError> { assert!(!op.is_multi_path(), "{op} is a multi-path LazyFS operation"); let from_rgx = from_rgx.into(); validate_command_argument("crash", "from_rgx", &from_rgx)?; Ok(Self { timing, op, from_rgx: Some(from_rgx), to_rgx: None, }) }
pub fn multi_path( timing: Timing, op: CrashOp, from_rgx: Option<impl Into<String>>, to_rgx: Option<impl Into<String>>, ) -> Result<Self, LazyFsCommandError> { assert!( op.is_multi_path(), "{op} is not a multi-path LazyFS operation" ); let from_rgx = from_rgx.map(Into::into); let to_rgx = to_rgx.map(Into::into); if from_rgx.is_none() && to_rgx.is_none() { return Err(LazyFsCommandError::MissingCrashPathRegex); } if let Some(from_rgx) = &from_rgx { validate_command_argument("crash", "from_rgx", from_rgx)?; } if let Some(to_rgx) = &to_rgx { validate_command_argument("crash", "to_rgx", to_rgx)?; } Ok(Self { timing, op, from_rgx, to_rgx, }) }
fn render(&self) -> String { let mut command = format!("lazyfs::crash::timing={}::op={}", self.timing, self.op); if let Some(from_rgx) = &self.from_rgx { command.push_str("::from_rgx="); command.push_str(from_rgx); } if let Some(to_rgx) = &self.to_rgx { command.push_str("::to_rgx="); command.push_str(to_rgx); } command }}
#[derive(Debug, Clone, PartialEq, Eq)]pub enum TornOpSplit { Parts(usize), PartsBytes(Vec<usize>),}
#[derive(Debug, Clone, PartialEq, Eq)]enum TornOpTarget { File(String), FileRegex(String),}
impl TornOpTarget { fn render(&self) -> String { match self { Self::File(file) => format!("file={file}"), Self::FileRegex(file_rgx) => format!("file_rgx={file_rgx}"), } }}
#[derive(Debug, Clone, PartialEq, Eq)]pub struct TornOpCommand { target: TornOpTarget, split: TornOpSplit, persist: Vec<usize>,}
impl TornOpCommand { pub fn new( file: impl AsRef<Path>, split: TornOpSplit, persist: Vec<usize>, ) -> Result<Self, LazyFsCommandError> { validate_torn_op(&split, &persist)?; let file = file.as_ref().to_string_lossy().into_owned(); validate_command_argument("torn-op", "file", &file)?; Ok(Self { target: TornOpTarget::File(file), split, persist, }) }
pub fn new_file_regex( file_rgx: impl Into<String>, split: TornOpSplit, persist: Vec<usize>, ) -> Result<Self, LazyFsCommandError> { let file_rgx = file_rgx.into(); validate_torn_op(&split, &persist)?; validate_command_argument("torn-op", "file_rgx", &file_rgx)?; Ok(Self { target: TornOpTarget::FileRegex(file_rgx), split, persist, }) }
fn render(&self) -> String { let mut command = format!("lazyfs::torn-op::{}", self.target.render()); command.push_str("::persist="); command.push_str(&render_list(&self.persist)); match &self.split { TornOpSplit::Parts(parts) => { command.push_str("::parts="); command.push_str(&parts.to_string()); } TornOpSplit::PartsBytes(parts_bytes) => { command.push_str("::parts_bytes="); command.push_str(&render_list(parts_bytes)); } } command }}
fn validate_torn_op(split: &TornOpSplit, persist: &[usize]) -> Result<(), LazyFsCommandError> { validate_persist("torn-op", persist)?; let max_part_index = match split { TornOpSplit::Parts(0) => return Err(LazyFsCommandError::ZeroTornOpParts), TornOpSplit::Parts(parts) => *parts, TornOpSplit::PartsBytes(parts_bytes) if parts_bytes.is_empty() => { return Err(LazyFsCommandError::EmptyTornOpPartBytes); } TornOpSplit::PartsBytes(parts_bytes) if parts_bytes.contains(&0) => { return Err(LazyFsCommandError::ZeroTornOpPartBytes); } TornOpSplit::PartsBytes(parts_bytes) => parts_bytes.len(), }; if let Some(index) = persist .iter() .copied() .find(|index| *index > max_part_index) { return Err(LazyFsCommandError::PersistIndexOutOfRange { command: "torn-op", index, max: max_part_index, }); } Ok(())}
#[derive(Debug, Clone, PartialEq, Eq)]pub struct TornSeqCommand { file: String, persist: Vec<usize>,}
impl TornSeqCommand { pub fn write( file: impl AsRef<Path>, mut persist: Vec<usize>, ) -> Result<Self, LazyFsCommandError> { validate_persist("torn-seq", &persist)?; let file = file.as_ref().to_string_lossy().into_owned(); validate_command_argument("torn-seq", "file", &file)?; persist.sort_unstable(); Ok(Self { file, persist }) }
fn render(&self) -> String { format!( "lazyfs::torn-seq::op=write::file={}::persist={}", self.file, render_list(&self.persist) ) }}
fn render_list(values: &[usize]) -> String { values .iter() .map(usize::to_string) .collect::<Vec<_>>() .join(",")}