device-scan: instead of panicking handle when bcachefs is given directory or file as argument

This commit is contained in:
John Titor 2025-10-04 12:27:07 +05:30
parent 9e4acfeae6
commit 36cc57f0ac
No known key found for this signature in database
GPG Key ID: 29B0514F4E3C1CC0

View File

@ -2,6 +2,7 @@ use std::{
ffi::{CStr, CString, c_char, c_int},
collections::HashMap,
env,
fs,
path::{Path, PathBuf},
str,
};
@ -132,6 +133,15 @@ fn devs_str_sbs_from_device(
device: &Path,
opts: &bch_opts
) -> anyhow::Result<Vec<(PathBuf, bch_sb_handle)>> {
if let Ok(metadata) = fs::metadata(device) {
if metadata.is_dir() {
return Err(anyhow::anyhow!("'{}' is a directory, not a block device", device.display()));
}
if metadata.is_file() {
return Err(anyhow::anyhow!("'{}' is a regular file, not a block device", device.display()));
}
}
let dev_sb = read_super_silent(device, *opts)?;
if dev_sb.sb().number_of_devices() == 1 {
@ -199,7 +209,11 @@ pub extern "C" fn bch2_scan_device_sbs(device: *const c_char, ret: *mut sb_names
// how to initialize to default/empty?
let opts = bch_bindgen::opts::parse_mount_opts(None, None, true).unwrap_or_default();
let sbs = scan_sbs(&device, &opts).unwrap();
let sbs = scan_sbs(&device, &opts)
.unwrap_or_else(|e| {
eprintln!("bcachefs ({}): error reading superblock: {}", device, e);
std::process::exit(-1);
});
let mut sbs = sbs.iter()
.map(|(name, sb)| sb_name {
@ -225,5 +239,8 @@ pub extern "C" fn bch2_scan_devices(device: *const c_char) -> *mut c_char {
// how to initialize to default/empty?
let opts = bch_bindgen::opts::parse_mount_opts(None, None, true).unwrap_or_default();
CString::new(scan_devices(&device, &opts).unwrap()).unwrap().into_raw()
CString::new(scan_devices(&device, &opts).unwrap_or_else(|e| {
eprintln!("bcachefs ({}): error reading superblock: {}", device, e);
std::process::exit(-1);
})).unwrap().into_raw()
}