From 3ac510f6a41feb1b695381fa30869d557c00b822 Mon Sep 17 00:00:00 2001 From: Thomas Bertschinger Date: Thu, 9 May 2024 15:14:06 -0600 Subject: add "bkey-type" option to list command Only bkeys of the specified type will be printed. Also, this reworks the error type in bch_bindgen to be able to represent other kinds of error than just "invalid btree id". Signed-off-by: Thomas Bertschinger Signed-off-by: Kent Overstreet --- bch_bindgen/src/lib.rs | 46 +++++++++++++++++++++++++++++++++++----------- src/commands/list.rs | 11 +++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/bch_bindgen/src/lib.rs b/bch_bindgen/src/lib.rs index d903735e..b31b1a61 100644 --- a/bch_bindgen/src/lib.rs +++ b/bch_bindgen/src/lib.rs @@ -79,19 +79,27 @@ pub fn path_to_cstr>(p: P) -> CString { use std::error::Error; #[derive(Debug)] -pub struct InvalidBtreeId; +pub enum BchToolsErr { + InvalidBtreeId, + InvalidBkeyType, + InvalidBpos, +} -impl fmt::Display for InvalidBtreeId { +impl fmt::Display for BchToolsErr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "invalid btree id") + match self { + BchToolsErr::InvalidBtreeId => write!(f, "invalid btree id"), + BchToolsErr::InvalidBkeyType => write!(f, "invalid bkey type"), + BchToolsErr::InvalidBpos => write!(f, "invalid bpos"), + } } } -impl Error for InvalidBtreeId { +impl Error for BchToolsErr { } impl FromStr for c::btree_id { - type Err = InvalidBtreeId; + type Err = BchToolsErr; fn from_str(s: &str) -> Result { let s = CString::new(s).unwrap(); @@ -101,7 +109,23 @@ impl FromStr for c::btree_id { if v >= 0 { Ok(unsafe { std::mem::transmute(v) }) } else { - Err(InvalidBtreeId) + Err(BchToolsErr::InvalidBtreeId) + } + } +} + +impl FromStr for c::bch_bkey_type { + type Err = BchToolsErr; + + fn from_str(s: &str) -> Result { + let s = CString::new(s).unwrap(); + let p = s.as_ptr(); + + let v = unsafe {c::match_string(c::bch2_bkey_types[..].as_ptr(), (-(1 as isize)) as usize, p)}; + if v >= 0 { + Ok(unsafe { std::mem::transmute(v) }) + } else { + Err(BchToolsErr::InvalidBkeyType) } } } @@ -134,7 +158,7 @@ impl fmt::Display for Bpos { } impl FromStr for c::bpos { - type Err = InvalidBtreeId; + type Err = BchToolsErr; fn from_str(s: &str) -> Result { if s == "POS_MIN" { @@ -150,12 +174,12 @@ impl FromStr for c::bpos { } let mut fields = s.split(':'); - let ino_str = fields.next().ok_or(InvalidBtreeId)?; - let off_str = fields.next().ok_or(InvalidBtreeId)?; + let ino_str = fields.next().ok_or(BchToolsErr::InvalidBpos)?; + let off_str = fields.next().ok_or(BchToolsErr::InvalidBpos)?; let snp_str = fields.next(); - let ino: u64 = ino_str.parse().map_err(|_| InvalidBtreeId)?; - let off: u64 = off_str.parse().map_err(|_| InvalidBtreeId)?; + let ino: u64 = ino_str.parse().map_err(|_| BchToolsErr::InvalidBpos)?; + let off: u64 = off_str.parse().map_err(|_| BchToolsErr::InvalidBpos)?; let snp: u32 = snp_str.map(|s| s.parse().ok()).flatten().unwrap_or(0); Ok(c::bpos { inode: ino, offset: off, snapshot: snp }) diff --git a/src/commands/list.rs b/src/commands/list.rs index 0ed6be05..fd61a516 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -21,6 +21,13 @@ fn list_keys(fs: &Fs, opt: Cli) -> anyhow::Result<()> { break; } + if let Some(ty) = opt.bkey_type { + if k.k.type_ != ty as u8 { + iter.advance(); + continue; + } + } + println!("{}", k.to_text(fs)); iter.advance(); } @@ -97,6 +104,10 @@ pub struct Cli { #[arg(short, long, default_value_t=bcachefs::btree_id::BTREE_ID_extents)] btree: bcachefs::btree_id, + /// Bkey type to list + #[arg(short='k', long)] + bkey_type: Option, + /// Btree depth to descend to (0 == leaves) #[arg(short, long, default_value_t=0)] level: u32, -- cgit v1.2.3