summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Mühlbacher <tmuehlbacher@posteo.net>2024-06-27 15:14:55 +0200
committerThomas Mühlbacher <tmuehlbacher@posteo.net>2024-06-29 00:11:07 +0200
commit5cce07e986012a32a9325f76658703f44b7579ad (patch)
tree4dc4316a4991ffb1f44cbbf97c7b6bc0c5a985e3 /src
parent176d76bceb0e89ce382f73dfe8f454278942a5bc (diff)
feat: use `ExitCode` over `std::process:exit()`
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 <tmuehlbacher@posteo.net>
Diffstat (limited to 'src')
-rw-r--r--src/bcachefs.rs28
-rw-r--r--src/commands/completions.rs3
-rw-r--r--src/commands/list.rs11
-rw-r--r--src/commands/mount.rs12
-rw-r--r--src/commands/subvolume.rs15
5 files changed, 30 insertions, 39 deletions
diff --git a/src/bcachefs.rs b/src/bcachefs.rs
index 643ac048..26422abd 100644
--- a/src/bcachefs.rs
+++ b/src/bcachefs.rs
@@ -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;
@@ -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") {
@@ -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() };
@@ -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()),
}
}
diff --git a/src/commands/completions.rs b/src/commands/completions.rs
index d4e98569..e05934ca 100644
--- a/src/commands/completions.rs
+++ b/src/commands/completions.rs
@@ -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
}
diff --git a/src/commands/list.rs b/src/commands/list.rs
index b7a42916..b1bf144a 100644
--- a/src/commands/list.rs
+++ b/src/commands/list.rs
@@ -1,3 +1,4 @@
+use anyhow::Result;
use bch_bindgen::bcachefs;
use bch_bindgen::bkey::BkeySC;
use bch_bindgen::btree::BtreeIter;
@@ -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;
@@ -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)
}
diff --git a/src/commands/mount.rs b/src/commands/mount.rs
index 35b7ae2a..235a1825 100644
--- a/src/commands/mount.rs
+++ b/src/commands/mount.rs
@@ -11,7 +11,7 @@ use std::{
use anyhow::{ensure, Result};
use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, opt_set, path_to_cstr};
use clap::Parser;
-use log::{debug, error, info};
+use log::{debug, info};
use uuid::Uuid;
use crate::{
@@ -372,7 +372,7 @@ fn cmd_mount_inner(cli: &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.
@@ -385,11 +385,5 @@ pub fn mount(mut argv: Vec<String>, symlink_cmd: Option<&str>) -> i32 {
// TODO: centralize this on the top level CLI
logging::setup(cli.quiet, cli.verbose, cli.colorize);
- if let Err(e) = cmd_mount_inner(&cli) {
- error!("Fatal error: {}", e);
- 1
- } else {
- info!("Successfully mounted");
- 0
- }
+ cmd_mount_inner(&cli)
}
diff --git a/src/commands/subvolume.rs b/src/commands/subvolume.rs
index b059ff4a..bb0141c4 100644
--- a/src/commands/subvolume.rs
+++ b/src/commands/subvolume.rs
@@ -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};
@@ -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 {
@@ -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 {
@@ -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(())
}