diff options
Diffstat (limited to 'rust-src/src')
-rw-r--r-- | rust-src/src/cmd_list.rs | 127 | ||||
-rw-r--r-- | rust-src/src/cmd_mount.rs | 13 | ||||
-rw-r--r-- | rust-src/src/filesystem.rs | 9 | ||||
-rw-r--r-- | rust-src/src/lib.rs | 11 |
4 files changed, 131 insertions, 29 deletions
diff --git a/rust-src/src/cmd_list.rs b/rust-src/src/cmd_list.rs new file mode 100644 index 00000000..ea4c93fa --- /dev/null +++ b/rust-src/src/cmd_list.rs @@ -0,0 +1,127 @@ +use atty::Stream; +use bch_bindgen::error; +use bch_bindgen::bcachefs; +use bch_bindgen::fs::Fs; +use bch_bindgen::btree::BtreeTrans; +use bch_bindgen::btree::BtreeIter; +use clap::Parser; +use colored::Colorize; +use std::ffi::{CStr, OsStr, c_int, c_char}; +use std::os::unix::ffi::OsStrExt; + +fn list_keys(fs: &Fs, opt: Cli) -> anyhow::Result<()> { + let trans = BtreeTrans::new(fs); + let mut iter = BtreeIter::new(&trans, opt.btree, opt.start, 1 << 11); + + while let Some(k) = iter.peek_and_restart()? { + unsafe { + if (*k.k).p > opt.end { + break; + } + } + + println!("{}", k.to_text(fs)); + + iter.advance(); + } + + Ok(()) +} + +fn list_btree_formats(fs: &Fs, opt: Cli) -> anyhow::Result<()> { + let trans = BtreeTrans::new(fs); + + Ok(()) +} + +fn list_btree_nodes(fs: &Fs, opt: Cli) -> anyhow::Result<()> { + let trans = BtreeTrans::new(fs); + + Ok(()) +} + +fn list_nodes_ondisk(fs: &Fs, opt: Cli) -> anyhow::Result<()> { + let trans = BtreeTrans::new(fs); + + Ok(()) +} + +fn list_nodes_keys(fs: &Fs, opt: Cli) -> anyhow::Result<()> { + let trans = BtreeTrans::new(fs); + + Ok(()) +} + +#[derive(Clone, clap::ValueEnum)] +enum Mode { + Keys, + Formats, + Nodes, + NodesOndisk, + NodesKeys, +} + +#[derive(Parser)] +struct Cli { + /// Btree to list from + #[arg(short, long, default_value_t=bcachefs::btree_id::BTREE_ID_extents)] + btree: bcachefs::btree_id, + + /// Btree depth to descend to (0 == leaves) + #[arg(short, long, default_value_t=0)] + level: u8, + + /// Start position to list from + #[arg(short, long, default_value="POS_MIN")] + start: bcachefs::bpos, + + /// End position + #[arg(short, long, default_value="SPOS_MAX")] + end: bcachefs::bpos, + + #[arg(short, long, default_value="keys")] + mode: Mode, + + /// Check (fsck) the filesystem first + #[arg(short, long, default_value_t=false)] + fsck: bool, + + /// Force color on/off. Default: autodetect tty + #[arg(short, long, action = clap::ArgAction::Set, default_value_t=atty::is(Stream::Stdout))] + colorize: bool, + + /// Verbose mode + #[arg(short, long, action = clap::ArgAction::Count)] + verbose: u8, + + #[arg(required(true))] + devices: Vec<std::path::PathBuf>, +} + +fn cmd_list_inner(opt: Cli) -> anyhow::Result<()> { + let fs_opts: bcachefs::bch_opts = Default::default(); + + let fs = Fs::open(&opt.devices, fs_opts)?; + + match opt.mode { + Mode::Keys => list_keys(&fs, opt), + Mode::Formats => list_btree_formats(&fs, opt), + Mode::Nodes => list_btree_nodes(&fs, opt), + Mode::NodesOndisk => list_nodes_ondisk(&fs, opt), + Mode::NodesKeys => list_nodes_keys(&fs, opt), + } +} + +#[no_mangle] +pub extern "C" fn cmd_rust_list(argc: c_int, argv: *const *const c_char) { + let argv: Vec<_> = (0..argc) + .map(|i| unsafe { CStr::from_ptr(*argv.add(i as usize)) }) + .map(|i| OsStr::from_bytes(i.to_bytes())) + .collect(); + + let opt = Cli::parse_from(argv); + colored::control::set_override(opt.colorize); + if let Err(e) = cmd_list_inner(opt) { + error!("Fatal error: {}", e); + } +} diff --git a/rust-src/src/cmd_mount.rs b/rust-src/src/cmd_mount.rs index 4bbe5fe7..1251d0d7 100644 --- a/rust-src/src/cmd_mount.rs +++ b/rust-src/src/cmd_mount.rs @@ -125,14 +125,6 @@ fn get_devices_by_uuid(uuid: Uuid) -> anyhow::Result<Vec<(PathBuf, bch_sb_handle Ok(devs) } -fn stdout_isatty() -> &'static str { - if atty::is(Stream::Stdout) { - "true" - } else { - "false" - } -} - /// Mount a bcachefs filesystem by its UUID. #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -159,10 +151,11 @@ struct Cli { options: String, /// Force color on/off. Default: autodetect tty - #[arg(short, long, action = clap::ArgAction::Set, default_value=stdout_isatty())] + #[arg(short, long, action = clap::ArgAction::Set, default_value_t=atty::is(Stream::Stdout))] colorize: bool, - #[arg(short = 'v', long, action = clap::ArgAction::Count)] + /// Verbose mode + #[arg(short, long, action = clap::ArgAction::Count)] verbose: u8, } diff --git a/rust-src/src/filesystem.rs b/rust-src/src/filesystem.rs deleted file mode 100644 index 336f847e..00000000 --- a/rust-src/src/filesystem.rs +++ /dev/null @@ -1,9 +0,0 @@ -extern "C" { - pub static stdout: *mut libc::FILE; -} -use bch_bindgen::{debug, info}; -use colored::Colorize; -use getset::{CopyGetters, Getters}; -use std::path::PathBuf; -use bcachefs::bch_sb_handle; - diff --git a/rust-src/src/lib.rs b/rust-src/src/lib.rs index 5feaa2e9..a33e3914 100644 --- a/rust-src/src/lib.rs +++ b/rust-src/src/lib.rs @@ -1,15 +1,6 @@ pub mod key; pub mod cmd_mount; - -pub mod err { - pub enum GError { - Unknown { - message: std::borrow::Cow<'static, String>, - }, - } - pub type GResult<T, E, OE> = ::core::result::Result<::core::result::Result<T, E>, OE>; - pub type Result<T, E> = GResult<T, E, GError>; -} +pub mod cmd_list; #[macro_export] macro_rules! c_str { |