Skip to content

Commit 3ac510f

Browse files
bertschingertKent Overstreet
authored andcommitted
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 <[email protected]> Signed-off-by: Kent Overstreet <[email protected]>
1 parent 477670f commit 3ac510f

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

bch_bindgen/src/lib.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,27 @@ pub fn path_to_cstr<P: AsRef<Path>>(p: P) -> CString {
7979
use std::error::Error;
8080

8181
#[derive(Debug)]
82-
pub struct InvalidBtreeId;
82+
pub enum BchToolsErr {
83+
InvalidBtreeId,
84+
InvalidBkeyType,
85+
InvalidBpos,
86+
}
8387

84-
impl fmt::Display for InvalidBtreeId {
88+
impl fmt::Display for BchToolsErr {
8589
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86-
write!(f, "invalid btree id")
90+
match self {
91+
BchToolsErr::InvalidBtreeId => write!(f, "invalid btree id"),
92+
BchToolsErr::InvalidBkeyType => write!(f, "invalid bkey type"),
93+
BchToolsErr::InvalidBpos => write!(f, "invalid bpos"),
94+
}
8795
}
8896
}
8997

90-
impl Error for InvalidBtreeId {
98+
impl Error for BchToolsErr {
9199
}
92100

93101
impl FromStr for c::btree_id {
94-
type Err = InvalidBtreeId;
102+
type Err = BchToolsErr;
95103

96104
fn from_str(s: &str) -> Result<Self, Self::Err> {
97105
let s = CString::new(s).unwrap();
@@ -101,7 +109,23 @@ impl FromStr for c::btree_id {
101109
if v >= 0 {
102110
Ok(unsafe { std::mem::transmute(v) })
103111
} else {
104-
Err(InvalidBtreeId)
112+
Err(BchToolsErr::InvalidBtreeId)
113+
}
114+
}
115+
}
116+
117+
impl FromStr for c::bch_bkey_type {
118+
type Err = BchToolsErr;
119+
120+
fn from_str(s: &str) -> Result<Self, Self::Err> {
121+
let s = CString::new(s).unwrap();
122+
let p = s.as_ptr();
123+
124+
let v = unsafe {c::match_string(c::bch2_bkey_types[..].as_ptr(), (-(1 as isize)) as usize, p)};
125+
if v >= 0 {
126+
Ok(unsafe { std::mem::transmute(v) })
127+
} else {
128+
Err(BchToolsErr::InvalidBkeyType)
105129
}
106130
}
107131
}
@@ -134,7 +158,7 @@ impl fmt::Display for Bpos {
134158
}
135159

136160
impl FromStr for c::bpos {
137-
type Err = InvalidBtreeId;
161+
type Err = BchToolsErr;
138162

139163
fn from_str(s: &str) -> Result<Self, Self::Err> {
140164
if s == "POS_MIN" {
@@ -150,12 +174,12 @@ impl FromStr for c::bpos {
150174
}
151175

152176
let mut fields = s.split(':');
153-
let ino_str = fields.next().ok_or(InvalidBtreeId)?;
154-
let off_str = fields.next().ok_or(InvalidBtreeId)?;
177+
let ino_str = fields.next().ok_or(BchToolsErr::InvalidBpos)?;
178+
let off_str = fields.next().ok_or(BchToolsErr::InvalidBpos)?;
155179
let snp_str = fields.next();
156180

157-
let ino: u64 = ino_str.parse().map_err(|_| InvalidBtreeId)?;
158-
let off: u64 = off_str.parse().map_err(|_| InvalidBtreeId)?;
181+
let ino: u64 = ino_str.parse().map_err(|_| BchToolsErr::InvalidBpos)?;
182+
let off: u64 = off_str.parse().map_err(|_| BchToolsErr::InvalidBpos)?;
159183
let snp: u32 = snp_str.map(|s| s.parse().ok()).flatten().unwrap_or(0);
160184

161185
Ok(c::bpos { inode: ino, offset: off, snapshot: snp })

src/commands/list.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ fn list_keys(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
2121
break;
2222
}
2323

24+
if let Some(ty) = opt.bkey_type {
25+
if k.k.type_ != ty as u8 {
26+
iter.advance();
27+
continue;
28+
}
29+
}
30+
2431
println!("{}", k.to_text(fs));
2532
iter.advance();
2633
}
@@ -97,6 +104,10 @@ pub struct Cli {
97104
#[arg(short, long, default_value_t=bcachefs::btree_id::BTREE_ID_extents)]
98105
btree: bcachefs::btree_id,
99106

107+
/// Bkey type to list
108+
#[arg(short='k', long)]
109+
bkey_type: Option<bcachefs::bch_bkey_type>,
110+
100111
/// Btree depth to descend to (0 == leaves)
101112
#[arg(short, long, default_value_t=0)]
102113
level: u32,

0 commit comments

Comments
 (0)