diff --git a/writeboost-cli/src/sub/check/mod.rs b/writeboost-cli/src/sub/check/mod.rs index ff4dd3a..dfa3f69 100644 --- a/writeboost-cli/src/sub/check/mod.rs +++ b/writeboost-cli/src/sub/check/mod.rs @@ -93,23 +93,35 @@ mod tests { pub struct CommandArgs { #[arg(help = "Path to the cache device")] cachedev: String, - #[arg(help = "Segment id")] - segid: u64, + #[arg(long, help = "Segment id to check")] + segid: Option, + #[arg(short, long, help = "Check all segments")] + all: bool, } pub fn run(args: CommandArgs) { let devname: String = args.cachedev; - let id = args.segid; - - match do_check(&devname, id) { - Ok(()) => {} - Err(e @ CheckError::NotInitialized) => { - // Since segments are zero-ed out at formatting, - // if the segment is all zeros, it is considered still unused. - eprintln!("{e}"); - } - Err(e) => { - panic!("{e}") + + let range = if let Some(seg_id) = args.segid { + seg_id..=seg_id + } else if args.all { + let cache_dev = CacheDevice::new(devname.to_owned()); + let nr_segs = cache_dev.nr_segments(); + 1..=nr_segs as u64 + } else { + panic!("either --segid or --all must be specified"); + }; + + for seg_id in range { + match do_check(&devname, seg_id) { + Ok(()) => {} + Err(CheckError::NotInitialized) => { + // Since segments are zero-ed out at formatting, + // if the segment is all zeros, it is considered still unused. + } + Err(e) => { + panic!("{e}") + } } } } diff --git a/writeboost-cli/src/utils.rs b/writeboost-cli/src/utils.rs index e883d7f..1e2bfbc 100644 --- a/writeboost-cli/src/utils.rs +++ b/writeboost-cli/src/utils.rs @@ -49,7 +49,7 @@ impl CacheDevice { } } - fn nr_segments(&self) -> u32 { + pub fn nr_segments(&self) -> u32 { ((self.dev.size() - (1 << 11)) / (1 << 10)) as u32 }