summaryrefslogtreecommitdiff
path: root/bch_bindgen
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@linux.dev>2024-03-02 18:41:55 -0500
committerKent Overstreet <kent.overstreet@linux.dev>2024-03-08 16:40:38 -0500
commit6a41851118277d8162b113097556a9b03059f9ad (patch)
treeab89d8635743fd6308e0413e7c71c5414c916502 /bch_bindgen
parentf4f87d9f76c575b2491fe83df66b0644f301f7f0 (diff)
path_to_cstr()
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'bch_bindgen')
-rw-r--r--bch_bindgen/src/lib.rs5
-rw-r--r--bch_bindgen/src/sb_io.rs15
2 files changed, 10 insertions, 10 deletions
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: AsRef<Path>>(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<bch_sb_handle> {
- 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<bch_sb_handle> {
- 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) };