]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - rust-src/src/cmd_mount.rs
cmd_mount: Use noxcl for opening block devices
[bcachefs-tools-debian] / rust-src / src / cmd_mount.rs
index 9d58cb3ea4eef100b3c4882ffa49bddc815d791c..eccfe6d0301d2ccb6d4e3d5d63cb3d2373fe2505 100644 (file)
@@ -1,13 +1,12 @@
 use atty::Stream;
-use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle};
+use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, opt_set};
 use log::{info, debug, error, LevelFilter};
-use clap::{Parser, Subcommand};
+use clap::{Parser};
 use uuid::Uuid;
 use std::path::PathBuf;
-use crate::{key, transform_c_args};
-use crate::key::KeyLoc;
-use crate::logger::SimpleLogger;
-use std::ffi::{CStr, CString, OsStr, c_int, c_char, c_void};
+use crate::key;
+use crate::key::KeyLocation;
+use std::ffi::{CString, c_int, c_char, c_void, OsStr};
 use std::os::unix::ffi::OsStrExt;
 
 fn mount_inner(
@@ -77,12 +76,11 @@ fn parse_mount_options(options: impl AsRef<str>) -> (Option<String>, libc::c_ulo
             }
         });
 
-    use itertools::Itertools;
     (
         if opts.len() == 0 {
             None
         } else {
-            Some(opts.iter().join(","))
+            Some(opts.join(","))
         },
         flags,
     )
@@ -106,7 +104,10 @@ fn read_super_silent(path: &std::path::PathBuf) -> anyhow::Result<bch_sb_handle>
     // Stop libbcachefs from spamming the output
     let _gag = gag::BufferRedirect::stdout().unwrap();
 
-    bch_bindgen::rs::read_super(&path)
+    let mut opts = bcachefs::bch_opts::default();
+    opt_set!(opts, noexcl, 1);
+
+    bch_bindgen::rs::read_super_opts(&path, opts)
 }
 
 fn get_devices_by_uuid(uuid: Uuid) -> anyhow::Result<Vec<(PathBuf, bch_sb_handle)>> {
@@ -137,7 +138,7 @@ pub struct Cli {
     /// "wait" - wait for password to become available before mounting;
     /// "ask" -  prompt the user for password;
     #[arg(short, long, default_value = "ask", verbatim_doc_comment)]
-    key_location:   KeyLoc,
+    key_location:   KeyLocation,
 
     /// Device, or UUID=<UUID>
     dev:            String,
@@ -190,7 +191,7 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> {
 
         for dev in opt.dev.split(':') {
             let dev = PathBuf::from(dev);
-            sbs.push(bch_bindgen::rs::read_super(&dev)?);
+            sbs.push(read_super_silent(&dev)?);
         }
 
         (opt.dev, sbs)
@@ -199,12 +200,7 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> {
     if sbs.len() == 0 {
         Err(anyhow::anyhow!("No device found from specified parameters"))?;
     } else if unsafe { bcachefs::bch2_sb_is_encrypted(sbs[0].sb) } {
-        let key = opt
-            .key_location
-            .0
-            .ok_or_else(|| anyhow::anyhow!("no keyoption specified for locked filesystem"))?;
-
-        key::prepare_key(&sbs[0], key)?;
+        key::prepare_key(&sbs[0], opt.key_location)?;
     }
 
     if let Some(mountpoint) = opt.mountpoint {
@@ -227,14 +223,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,