diff options
author | Thomas Bertschinger <tahbertschinger@gmail.com> | 2024-01-07 18:36:33 -0700 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2024-01-07 21:38:31 -0500 |
commit | 9a9af6e9e6560407326b37345ebe434a0839420f (patch) | |
tree | e105d7ed49fb20988e9e9c6c1a94fc2d6bb63475 /rust-src/src/cmd_main.rs | |
parent | b90031efaa4500b8ce5ffc55e9d1ed716196b814 (diff) |
create common entry point for Rust commands
The bcachefs sub-commands that are implemented in Rust (completions,
list, and mount) had separate entrypoints and thus had some differences
in behavior.
This introduces a common entry point for the Rust sub-commands. This
reduces duplicate boilerplate code like parsing argv and setting up
logging, and will facilitate converting more sub-commands to Rust in
the future.
An immediate benefit is that this fixes an issue with `bcachefs list`
not reporting errors:
before:
$ bcachefs list /dev/typo
$ echo $?
0
after:
$ bcachefs list /dev/typo
ERROR - bcachefs_rust::cmd_list: Fatal error: "No such file or directory"
$ echo $?
1
Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'rust-src/src/cmd_main.rs')
-rw-r--r-- | rust-src/src/cmd_main.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/rust-src/src/cmd_main.rs b/rust-src/src/cmd_main.rs new file mode 100644 index 00000000..baedc851 --- /dev/null +++ b/rust-src/src/cmd_main.rs @@ -0,0 +1,34 @@ +use log::{error, LevelFilter}; +use std::ffi::{CStr, c_int, c_char}; +use crate::transform_c_args; +use crate::logger::SimpleLogger; +use crate::cmd_completions::cmd_completions; +use crate::cmd_list::cmd_list; +use crate::cmd_mount::cmd_mount; + +#[no_mangle] +pub extern "C" fn rust_main(argc: c_int, argv: *const *const c_char, cmd: *const c_char) -> c_int { + transform_c_args!(argv, argc, argv); + + log::set_boxed_logger(Box::new(SimpleLogger)).unwrap(); + log::set_max_level(LevelFilter::Warn); + + let cmd: &CStr = unsafe { CStr::from_ptr(cmd) }; + let cmd = match cmd.to_str() { + Ok(c) => c, + Err(e) => { + error!("could not parse command: {}", e); + return 1; + } + }; + + match cmd { + "completions" => cmd_completions(argv), + "list" => cmd_list(argv), + "mount" => cmd_mount(argv), + _ => { + error!("unknown command: {}", cmd); + 1 + } + } +} |