mirror of
https://github.com/koverstreet/bcachefs-tools.git
synced 2025-03-10 00:00:04 +03:00
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>
This commit is contained in:
parent
176d76bceb
commit
5cce07e986
@ -3,7 +3,10 @@ mod key;
|
|||||||
mod logging;
|
mod logging;
|
||||||
mod wrappers;
|
mod wrappers;
|
||||||
|
|
||||||
use std::ffi::{c_char, CString};
|
use std::{
|
||||||
|
ffi::{c_char, CString},
|
||||||
|
process::{ExitCode, Termination},
|
||||||
|
};
|
||||||
|
|
||||||
use bch_bindgen::c;
|
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 args: Vec<String> = std::env::args().collect();
|
||||||
|
|
||||||
let symlink_cmd: Option<&str> = if args[0].contains("mkfs") {
|
let symlink_cmd: Option<&str> = if args[0].contains("mkfs") {
|
||||||
@ -89,7 +92,7 @@ fn main() {
|
|||||||
if symlink_cmd.is_none() && args.len() < 2 {
|
if symlink_cmd.is_none() && args.len() < 2 {
|
||||||
println!("missing command");
|
println!("missing command");
|
||||||
unsafe { c::bcachefs_usage() };
|
unsafe { c::bcachefs_usage() };
|
||||||
std::process::exit(1);
|
return ExitCode::from(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { c::raid_init() };
|
unsafe { c::raid_init() };
|
||||||
@ -99,15 +102,14 @@ fn main() {
|
|||||||
None => args[1].as_str(),
|
None => args[1].as_str(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let ret = match cmd {
|
match cmd {
|
||||||
"completions" => commands::completions(args[1..].to_vec()),
|
"completions" => {
|
||||||
"list" => commands::list(args[1..].to_vec()),
|
commands::completions(args[1..].to_vec());
|
||||||
"mount" => commands::mount(args, symlink_cmd),
|
ExitCode::SUCCESS
|
||||||
"subvolume" => commands::subvolume(args[1..].to_vec()),
|
}
|
||||||
_ => handle_c_command(args, symlink_cmd),
|
"list" => commands::list(args[1..].to_vec()).report(),
|
||||||
};
|
"mount" => commands::mount(args, symlink_cmd).report(),
|
||||||
|
"subvolume" => commands::subvolume(args[1..].to_vec()).report(),
|
||||||
if ret != 0 {
|
_ => ExitCode::from(u8::try_from(handle_c_command(args, symlink_cmd)).unwrap()),
|
||||||
std::process::exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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());
|
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);
|
let cli = Cli::parse_from(argv);
|
||||||
print_completions(cli.shell, &mut super::Cli::command());
|
print_completions(cli.shell, &mut super::Cli::command());
|
||||||
0
|
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use anyhow::Result;
|
||||||
use bch_bindgen::bcachefs;
|
use bch_bindgen::bcachefs;
|
||||||
use bch_bindgen::bkey::BkeySC;
|
use bch_bindgen::bkey::BkeySC;
|
||||||
use bch_bindgen::btree::BtreeIter;
|
use bch_bindgen::btree::BtreeIter;
|
||||||
@ -7,7 +8,6 @@ use bch_bindgen::btree::BtreeTrans;
|
|||||||
use bch_bindgen::fs::Fs;
|
use bch_bindgen::fs::Fs;
|
||||||
use bch_bindgen::opt_set;
|
use bch_bindgen::opt_set;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use log::error;
|
|
||||||
use std::io::{stdout, IsTerminal};
|
use std::io::{stdout, IsTerminal};
|
||||||
|
|
||||||
use crate::logging;
|
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);
|
let opt = Cli::parse_from(argv);
|
||||||
|
|
||||||
// TODO: centralize this on the top level CLI
|
// TODO: centralize this on the top level CLI
|
||||||
logging::setup(opt.quiet, opt.verbose, opt.colorize);
|
logging::setup(opt.quiet, opt.verbose, opt.colorize);
|
||||||
|
|
||||||
if let Err(e) = cmd_list_inner(&opt) {
|
cmd_list_inner(&opt)
|
||||||
error!("Fatal error: {}", e);
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ use std::{
|
|||||||
use anyhow::{ensure, Result};
|
use anyhow::{ensure, Result};
|
||||||
use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, opt_set, path_to_cstr};
|
use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, opt_set, path_to_cstr};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use log::{debug, error, info};
|
use log::{debug, info};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
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
|
// 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
|
// 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.
|
// ("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
|
// TODO: centralize this on the top level CLI
|
||||||
logging::setup(cli.quiet, cli.verbose, cli.colorize);
|
logging::setup(cli.quiet, cli.verbose, cli.colorize);
|
||||||
|
|
||||||
if let Err(e) = cmd_mount_inner(&cli) {
|
cmd_mount_inner(&cli)
|
||||||
error!("Fatal error: {}", e);
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
info!("Successfully mounted");
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use std::{env, path::PathBuf};
|
use std::{env, path::PathBuf};
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
use bch_bindgen::c::BCH_SUBVOL_SNAPSHOT_RO;
|
use bch_bindgen::c::BCH_SUBVOL_SNAPSHOT_RO;
|
||||||
use clap::{Parser, Subcommand};
|
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);
|
let cli = Cli::parse_from(argv);
|
||||||
|
|
||||||
match cli.subcommands {
|
match cli.subcommands {
|
||||||
@ -47,25 +48,25 @@ pub fn subvolume(argv: Vec<String>) -> i32 {
|
|||||||
} else {
|
} else {
|
||||||
env::current_dir()
|
env::current_dir()
|
||||||
.map(|p| p.join(target))
|
.map(|p| p.join(target))
|
||||||
.expect("unable to get current directory")
|
.context("unable to get current directory")?
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(dirname) = target.parent() {
|
if let Some(dirname) = target.parent() {
|
||||||
let fs = unsafe { BcachefsHandle::open(dirname) };
|
let fs = unsafe { BcachefsHandle::open(dirname) };
|
||||||
fs.create_subvolume(target)
|
fs.create_subvolume(target)
|
||||||
.expect("Failed to create the subvolume");
|
.context("Failed to create the subvolume")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Subcommands::Delete { target } => {
|
Subcommands::Delete { target } => {
|
||||||
let target = target
|
let target = target
|
||||||
.canonicalize()
|
.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() {
|
if let Some(dirname) = target.parent() {
|
||||||
let fs = unsafe { BcachefsHandle::open(dirname) };
|
let fs = unsafe { BcachefsHandle::open(dirname) };
|
||||||
fs.delete_subvolume(target)
|
fs.delete_subvolume(target)
|
||||||
.expect("Failed to delete the subvolume");
|
.context("Failed to delete the subvolume")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Subcommands::Snapshot {
|
Subcommands::Snapshot {
|
||||||
@ -91,10 +92,10 @@ pub fn subvolume(argv: Vec<String>) -> i32 {
|
|||||||
source,
|
source,
|
||||||
dest,
|
dest,
|
||||||
)
|
)
|
||||||
.expect("Failed to snapshot the subvolume");
|
.context("Failed to snapshot the subvolume")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
0
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user