diff --git a/bch_bindgen/src/lib.rs b/bch_bindgen/src/lib.rs index ea49e60b..d903735e 100644 --- a/bch_bindgen/src/lib.rs +++ b/bch_bindgen/src/lib.rs @@ -70,6 +70,11 @@ impl fmt::Display for c::btree_id { use std::str::FromStr; use std::ffi::CString; +use std::{path::Path,os::unix::ffi::OsStrExt}; + +pub fn path_to_cstr>(p: P) -> CString { + CString::new(p.as_ref().as_os_str().as_bytes()).unwrap() +} use std::error::Error; diff --git a/bch_bindgen/src/sb_io.rs b/bch_bindgen/src/sb_io.rs index 172584d7..44278f85 100644 --- a/bch_bindgen/src/sb_io.rs +++ b/bch_bindgen/src/sb_io.rs @@ -1,4 +1,5 @@ use anyhow::anyhow; +use crate::path_to_cstr; use crate::bcachefs; use crate::bcachefs::*; use crate::errcode::bch_errcode; @@ -7,13 +8,10 @@ pub fn read_super_opts( path: &std::path::Path, mut opts: bch_opts, ) -> anyhow::Result { - use std::os::unix::ffi::OsStrExt; - let path = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap(); - + let path = path_to_cstr(path); let mut sb = std::mem::MaybeUninit::zeroed(); - let ret = - unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) }; + let ret = unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) }; if ret != 0 { let err: bch_errcode = unsafe { ::std::mem::transmute(ret) }; @@ -32,13 +30,10 @@ pub fn read_super_silent( path: &std::path::Path, mut opts: bch_opts, ) -> anyhow::Result { - use std::os::unix::ffi::OsStrExt; - let path = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap(); - + let path = path_to_cstr(path); let mut sb = std::mem::MaybeUninit::zeroed(); - let ret = - unsafe { crate::bcachefs::bch2_read_super_silent(path.as_ptr(), &mut opts, sb.as_mut_ptr()) }; + let ret = unsafe { crate::bcachefs::bch2_read_super_silent(path.as_ptr(), &mut opts, sb.as_mut_ptr()) }; if ret != 0 { let err: bch_errcode = unsafe { ::std::mem::transmute(ret) }; diff --git a/src/commands/cmd_mount.rs b/src/commands/cmd_mount.rs index 63998456..4f9cc9fa 100644 --- a/src/commands/cmd_mount.rs +++ b/src/commands/cmd_mount.rs @@ -1,4 +1,4 @@ -use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, opt_set}; +use bch_bindgen::{path_to_cstr, bcachefs, bcachefs::bch_sb_handle, opt_set}; use log::{info, debug, error, LevelFilter}; use clap::Parser; use uuid::Uuid; @@ -7,7 +7,6 @@ use std::path::PathBuf; use crate::key; use crate::key::UnlockPolicy; use std::ffi::{CString, c_char, c_void}; -use std::os::unix::ffi::OsStrExt; fn mount_inner( src: String, @@ -19,7 +18,7 @@ fn mount_inner( // bind the CStrings to keep them alive let src = CString::new(src)?; - let target = CString::new(target.as_ref().as_os_str().as_bytes())?; + let target = path_to_cstr(target); let data = data.map(CString::new).transpose()?; let fstype = CString::new(fstype)?; diff --git a/src/wrappers/handle.rs b/src/wrappers/handle.rs index 60bdedb7..50023745 100644 --- a/src/wrappers/handle.rs +++ b/src/wrappers/handle.rs @@ -1,6 +1,7 @@ -use std::{path::Path, os::unix::ffi::OsStrExt, ffi::CString}; +use std::path::Path; use bch_bindgen::c::{bchfs_handle, BCH_IOCTL_SUBVOLUME_CREATE, BCH_IOCTL_SUBVOLUME_DESTROY, bch_ioctl_subvolume, bcache_fs_open, BCH_SUBVOL_SNAPSHOT_CREATE, bcache_fs_close}; +use bch_bindgen::path_to_cstr; use errno::Errno; /// A handle to a bcachefs filesystem @@ -13,7 +14,7 @@ impl BcachefsHandle { /// Opens a bcachefs filesystem and returns its handle /// TODO(raitobezarius): how can this not be faillible? pub(crate) unsafe fn open>(path: P) -> Self { - let path = CString::new(path.as_ref().as_os_str().as_bytes()).expect("Failed to cast path into a C-style string"); + let path = path_to_cstr(path); Self { inner: bcache_fs_open(path.as_ptr()) } @@ -59,7 +60,7 @@ impl BcachefsHandle { /// Create a subvolume for this bcachefs filesystem /// at the given path pub fn create_subvolume>(&self, dst: P) -> Result<(), Errno> { - let dst = CString::new(dst.as_ref().as_os_str().as_bytes()).expect("Failed to cast destination path for subvolume in a C-style string"); + let dst = path_to_cstr(dst); self.ioctl(BcachefsIoctl::SubvolumeCreate, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume { dirfd: libc::AT_FDCWD as u32, mode: 0o777, @@ -71,7 +72,7 @@ impl BcachefsHandle { /// Delete the subvolume at the given path /// for this bcachefs filesystem pub fn delete_subvolume>(&self, dst: P) -> Result<(), Errno> { - let dst = CString::new(dst.as_ref().as_os_str().as_bytes()).expect("Failed to cast destination path for subvolume in a C-style string"); + let dst = path_to_cstr(dst); self.ioctl(BcachefsIoctl::SubvolumeDestroy, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume { dirfd: libc::AT_FDCWD as u32, mode: 0o777, @@ -83,8 +84,8 @@ impl BcachefsHandle { /// Snapshot a subvolume for this bcachefs filesystem /// at the given path pub fn snapshot_subvolume>(&self, extra_flags: u32, src: Option

, dst: P) -> Result<(), Errno> { - let src = src.map(|src| CString::new(src.as_ref().as_os_str().as_bytes()).expect("Failed to cast source path for subvolume in a C-style string")); - let dst = CString::new(dst.as_ref().as_os_str().as_bytes()).expect("Failed to cast destination path for subvolume in a C-style string"); + let src = src.map(|src| path_to_cstr(src)); + let dst = path_to_cstr(dst); let res = self.ioctl(BcachefsIoctl::SubvolumeCreate, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume { flags: BCH_SUBVOL_SNAPSHOT_CREATE | extra_flags,