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 <tahbertschinger@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Thomas Bertschinger 2024-05-09 15:14:06 -06:00 committed by Kent Overstreet
parent 477670f481
commit 3ac510f6a4
2 changed files with 46 additions and 11 deletions

View File

@ -79,19 +79,27 @@ pub fn path_to_cstr<P: AsRef<Path>>(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<Self, Self::Err> {
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<Self, Self::Err> {
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<Self, Self::Err> {
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 })

View File

@ -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<bcachefs::bch_bkey_type>,
/// Btree depth to descend to (0 == leaves)
#[arg(short, long, default_value_t=0)]
level: u32,