linux-bcachefs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH TOOLS] add "bkey-type" option to list command
@ 2024-05-09 21:14 Thomas Bertschinger
  0 siblings, 0 replies; only message in thread
From: Thomas Bertschinger @ 2024-05-09 21:14 UTC (permalink / raw)
  To: kent.overstreet, linux-bcachefs, bfoster; +Cc: Thomas Bertschinger

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2024-05-09 21:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-09 21:14 [PATCH TOOLS] add "bkey-type" option to list command Thomas Bertschinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).