Skip to content

Commit

Permalink
feat: use ExitCode over std::process:exit()
Browse files Browse the repository at this point in the history
Should provide us with better outputs on process failure, also makes
unwinding better and is generally recommended over `exit()`.

Signed-off-by: Thomas Mühlbacher <[email protected]>
  • Loading branch information
tmuehlbacher committed Jun 28, 2024
1 parent 2dfd0a6 commit 81684ff
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
28 changes: 15 additions & 13 deletions src/bcachefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ mod key;
mod logging;
mod wrappers;

use std::ffi::{c_char, CString};
use std::{
ffi::{c_char, CString},
process::{ExitCode, Termination},
};

use bch_bindgen::c;

Expand Down Expand Up @@ -71,7 +74,7 @@ fn handle_c_command(mut argv: Vec<String>, symlink_cmd: Option<&str>) -> i32 {
}
}

fn main() {
fn main() -> ExitCode {
let args: Vec<String> = std::env::args().collect();

let symlink_cmd: Option<&str> = if args[0].contains("mkfs") {
Expand All @@ -89,7 +92,7 @@ fn main() {
if symlink_cmd.is_none() && args.len() < 2 {
println!("missing command");
unsafe { c::bcachefs_usage() };
std::process::exit(1);
return ExitCode::from(1);
}

unsafe { c::raid_init() };
Expand All @@ -99,15 +102,14 @@ fn main() {
None => args[1].as_str(),
};

let ret = match cmd {
"completions" => commands::completions(args[1..].to_vec()),
"list" => commands::list(args[1..].to_vec()),
"mount" => commands::mount(args, symlink_cmd),
"subvolume" => commands::subvolume(args[1..].to_vec()),
_ => handle_c_command(args, symlink_cmd),
};

if ret != 0 {
std::process::exit(1);
match cmd {
"completions" => {
commands::completions(args[1..].to_vec());
ExitCode::SUCCESS
}
"list" => commands::list(args[1..].to_vec()).report(),
"mount" => commands::mount(args, symlink_cmd).report(),
"subvolume" => commands::subvolume(args[1..].to_vec()).report(),
_ => ExitCode::from(u8::try_from(handle_c_command(args, symlink_cmd)).unwrap()),
}
}
3 changes: 1 addition & 2 deletions src/commands/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

pub fn completions(argv: Vec<String>) -> i32 {
pub fn completions(argv: Vec<String>) {
let cli = Cli::parse_from(argv);
print_completions(cli.shell, &mut super::Cli::command());
0
}
11 changes: 3 additions & 8 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use bch_bindgen::bcachefs;
use bch_bindgen::bkey::BkeySC;
use bch_bindgen::btree::BtreeIter;
Expand All @@ -7,7 +8,6 @@ use bch_bindgen::btree::BtreeTrans;
use bch_bindgen::fs::Fs;
use bch_bindgen::opt_set;
use clap::Parser;
use log::error;
use std::io::{stdout, IsTerminal};

use crate::logging;
Expand Down Expand Up @@ -201,16 +201,11 @@ fn cmd_list_inner(opt: &Cli) -> anyhow::Result<()> {
}
}

pub fn list(argv: Vec<String>) -> i32 {
pub fn list(argv: Vec<String>) -> Result<()> {
let opt = Cli::parse_from(argv);

// TODO: centralize this on the top level CLI
logging::setup(opt.quiet, opt.verbose, opt.colorize);

if let Err(e) = cmd_list_inner(&opt) {
error!("Fatal error: {}", e);
1
} else {
0
}
cmd_list_inner(&opt)
}
10 changes: 2 additions & 8 deletions src/commands/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ fn cmd_mount_inner(opt: Cli) -> Result<()> {
}
}

pub fn mount(mut argv: Vec<String>, symlink_cmd: Option<&str>) -> i32 {
pub fn mount(mut argv: Vec<String>, symlink_cmd: Option<&str>) -> Result<()> {
// If the bcachefs tool is being called as "bcachefs mount dev ..." (as opposed to via a
// symlink like "/usr/sbin/mount.bcachefs dev ...", then we need to pop the 0th argument
// ("bcachefs") since the CLI parser here expects the device at position 1.
Expand All @@ -394,11 +394,5 @@ pub fn mount(mut argv: Vec<String>, symlink_cmd: Option<&str>) -> i32 {
// TODO: centralize this on the top level CLI
logging::setup(opt.quiet, opt.verbose, opt.colorize);

if let Err(e) = cmd_mount_inner(opt) {
error!("Fatal error: {}", e);
1
} else {
info!("Successfully mounted");
0
}
cmd_mount_inner(opt)
}
15 changes: 8 additions & 7 deletions src/commands/subvolume.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{env, path::PathBuf};

use anyhow::{Context, Result};
use bch_bindgen::c::BCH_SUBVOL_SNAPSHOT_RO;
use clap::{Parser, Subcommand};

Expand Down Expand Up @@ -36,7 +37,7 @@ enum Subcommands {
},
}

pub fn subvolume(argv: Vec<String>) -> i32 {
pub fn subvolume(argv: Vec<String>) -> Result<()> {
let cli = Cli::parse_from(argv);

match cli.subcommands {
Expand All @@ -47,25 +48,25 @@ pub fn subvolume(argv: Vec<String>) -> i32 {
} else {
env::current_dir()
.map(|p| p.join(target))
.expect("unable to get current directory")
.context("unable to get current directory")?
};

if let Some(dirname) = target.parent() {
let fs = unsafe { BcachefsHandle::open(dirname) };
fs.create_subvolume(target)
.expect("Failed to create the subvolume");
.context("Failed to create the subvolume")?;
}
}
}
Subcommands::Delete { target } => {
let target = target
.canonicalize()
.expect("subvolume path does not exist or can not be canonicalized");
.context("subvolume path does not exist or can not be canonicalized")?;

if let Some(dirname) = target.parent() {
let fs = unsafe { BcachefsHandle::open(dirname) };
fs.delete_subvolume(target)
.expect("Failed to delete the subvolume");
.context("Failed to delete the subvolume")?;
}
}
Subcommands::Snapshot {
Expand All @@ -91,10 +92,10 @@ pub fn subvolume(argv: Vec<String>) -> i32 {
source,
dest,
)
.expect("Failed to snapshot the subvolume");
.context("Failed to snapshot the subvolume")?;
}
}
}

0
Ok(())
}

0 comments on commit 81684ff

Please sign in to comment.