create common entry point for Rust commands
PATCH 1/1 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
--- bcachefs.c | 14 +++++--------- cmds.h | 5 ++--- rust-src/src/cmd_completions.rs | 8 ++------ rust-src/src/cmd_list.rs | 11 +++++------ rust-src/src/cmd_main.rs | 34 +++++++++++++++++++++++++++++++++ rust-src/src/cmd_mount.rs | 12 +++--------- rust-src/src/lib.rs | 1 + 7 files changed, 52 insertions(+), 33 deletions(-) create mode 100644 rust-src/src/cmd_main.rs
bcachefs.c +5 -9
@@ -207,7 +207,7 @@ int main(int argc, char *argv[])#ifndef BCACHEFS_NO_RUST if (strstr (full_cmd,"mount" )) -return cmd_mount (argc, argv); +return rust_main (argc, argv,"mount" );#endif setvbuf (stdout,NULL , _IOLBF,0 );
@@ -265,10 +265,6 @@ int main(int argc, char *argv[])if (!strcmp (cmd,"dump" ))return cmd_dump (argc, argv); -#ifndef BCACHEFS_NO_RUST -if (!strcmp (cmd,"list" )) -return cmd_list (argc, argv); -#endif if (!strcmp (cmd,"list_journal" ))return cmd_list_journal (argc, argv);if (!strcmp (cmd,"kill_btree_node" ))
@@ -277,10 +273,10 @@ int main(int argc, char *argv[])if (!strcmp (cmd,"setattr" ))return cmd_setattr (argc, argv);#ifndef BCACHEFS_NO_RUST -if (!strcmp (cmd,"mount" )) -return cmd_mount (argc, argv); -if (strstr (cmd,"completions" )) -return cmd_completions (argc, argv); +if (!strcmp (cmd,"list" ) || + !strcmp (cmd,"mount" ) || + !strcmp (cmd,"completions" )) +return rust_main (argc, argv, cmd);#endif #ifdef BCACHEFS_FUSE
cmds.h +2 -3
@@ -37,7 +37,6 @@ int cmd_remove_passphrase(int argc, char *argv[]);int cmd_fsck (int argc,char *argv[]);int cmd_dump (int argc,char *argv[]); -int cmd_list (int argc,char *argv[]);int cmd_list_journal (int argc,char *argv[]);int cmd_kill_btree_node (int argc,char *argv[]);
@@ -54,7 +53,7 @@ int cmd_subvolume_delete(int argc, char *argv[]);int cmd_subvolume_snapshot (int argc,char *argv[]);int cmd_fusemount (int argc,char *argv[]); -int cmd_mount (int argc,char *argv[]); -int cmd_completions (int argc,char *argv[]); + +int rust_main (int argc,char *argv[],char *cmd);#endif /* _CMDS_H */
rust-src/src/cmd_completions.rs +2 -6
@@ -1,7 +1,6 @@ -use crate ::transform_c_args;use clap::{Command ,CommandFactory ,Parser };use clap_complete::{generate,Generator ,Shell }; -use std::ffi::{c_char, c_int}; +use std::ffi::{c_int,OsStr };use std::io;/// Generate shell completions
@@ -14,10 +13,7 @@ fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {generate (gen, cmd, cmd.get_name ().to_string (), &mut io::stdout ()); } -#[no_mangle] -#[allow(clippy::not_unsafe_ptr_arg_deref)] -pub extern "C" fn cmd_completions (argc:c_int , argv: *const *const c_char ) ->c_int { -transform_c_args! (argv, argc, argv); +pub fn cmd_completions (argv:Vec <&OsStr >) ->c_int {let cli =Cli ::parse_from (argv);print_completions (cli.shell, &mut super ::Cli ::command ());0
rust-src/src/cmd_list.rs +5 -6
@@ -9,8 +9,7 @@ use bch_bindgen::btree::BtreeIter;use bch_bindgen::btree::BtreeNodeIter ;use bch_bindgen::btree::BtreeIterFlags ;use clap::{Parser }; -use std::ffi::{c_int, c_char}; -use crate ::transform_c_args; +use std::ffi::{c_int,OsStr };fn list_keys (fs: &Fs , opt:Cli ) -> anyhow::Result <()> {let trans =BtreeTrans ::new (fs);
@@ -158,13 +157,13 @@ fn cmd_list_inner(opt: Cli) -> anyhow::Result<()> { } } -#[no_mangle] -#[allow(clippy::not_unsafe_ptr_arg_deref)] -pub extern "C" fn cmd_list (argc:c_int , argv: *const *const c_char ) { -transform_c_args! (argv, argc, argv); +pub fn cmd_list (argv:Vec <&OsStr >) ->c_int {let opt =Cli ::parse_from (argv); colored::control::set_override (opt.colorize);if let Err (e) =cmd_list_inner (opt) {error! ("Fatal error: {}" , e); +1 + }else { +0 } }
rust-src/src/cmd_main.rs new file +34 -0
@@ -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 + } + } +}
rust-src/src/cmd_mount.rs +3 -9
@@ -4,10 +4,9 @@ use log::{info, debug, error, LevelFilter};use clap::{Parser };use uuid::Uuid ;use std::path::PathBuf ; -use crate ::{key, transform_c_args}; +use crate ::key;use crate ::key::KeyLocation ; -use crate ::logger::SimpleLogger ; -use std::ffi::{CString , c_int, c_char, c_void}; +use std::ffi::{CString , c_int, c_char, c_void,OsStr };use std::os::unix::ffi::OsStrExt ; fn mount_inner(
@@ -222,14 +221,9 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> {Ok (()) } -#[no_mangle] -#[allow(clippy::not_unsafe_ptr_arg_deref)] -pub extern "C" fn cmd_mount (argc:c_int , argv: *const *const c_char ) ->c_int { -transform_c_args! (argv, argc, argv); +pub fn cmd_mount (argv:Vec <&OsStr >) ->c_int {let opt =Cli ::parse_from (argv); - log::set_boxed_logger (Box ::new (SimpleLogger )).unwrap (); -// @TODO : more granular log levels via mount option log::set_max_level (match opt.verbose {0 =>LevelFilter ::Warn ,
rust-src/src/lib.rs +1 -0
@@ -2,6 +2,7 @@ use clap::Subcommand;pub mod key;pub mod logger; +pub mod cmd_main;pub mod cmd_mount;pub mod cmd_list;pub mod cmd_completions;
Signature · 1 line
2.43.0
Quoted 18 lines · Thomas Bertschinger
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 listnot reporting errors:before:
$ bcachefs list /dev/typo $ echo $? 0
Quoted 6 lines
after:
$ bcachefs list /dev/typo ERROR - bcachefs_rust::cmd_list: Fatal error: "No such file or directory" $ echo $? 1
Applying this
If you feel like going the extra mile, maybe we could convert the top level main() to Rust, that'd further improve our helptext...
Quoted 4 lines · Kent Overstreet
Applying this
If you feel like going the extra mile, maybe we could convert the top level
main()to Rust, that'd further improve our helptext...
I thought about that, I wasn't sure if it made sense to do it now or wait until more stuff is done on the Rust side. Are there plans to move all the commands over to Rust at some point?
I'd be willing to give this a shot and send a patch if it'd be useful.
Is it important to keep a C top level main() around for compiling without Rust, BCACHEFS_NO_RUST defined? Or is Rust considered a hard requirement at this point?
Quoted 10 lines · Thomas Bertschinger
Quoted 4 lines · Kent Overstreet
Applying this
If you feel like going the extra mile, maybe we could convert the top level
main()to Rust, that'd further improve our helptext...I thought about that, I wasn't sure if it made sense to do it now or wait until more stuff is done on the Rust side. Are there plans to move all the commands over to Rust at some point?
Quoted 5 lines
I'd be willing to give this a shot and send a patch if it'd be useful.
Is it important to keep a C top level
main()around for compiling without Rust,BCACHEFS_NO_RUSTdefined? Or is Rust considered a hard requirement at this point?
It will be soon enough anyways, we can drop that now.