diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/device_scan.rs | 57 | ||||
-rw-r--r-- | src/rust_to_c.h | 17 |
2 files changed, 73 insertions, 1 deletions
diff --git a/src/device_scan.rs b/src/device_scan.rs index 4bed2d13..a6b62290 100644 --- a/src/device_scan.rs +++ b/src/device_scan.rs @@ -1,4 +1,5 @@ use std::{ + ffi::{CStr, CString, c_char, c_int}, collections::HashMap, env, path::{Path, PathBuf}, @@ -6,7 +7,12 @@ use std::{ }; use anyhow::Result; -use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, opt_set}; +use bch_bindgen::{bcachefs, opt_set}; +use bcachefs::{ + bch_sb_handle, + sb_name, + sb_names, +}; use bcachefs::bch_opts; use uuid::Uuid; use log::debug; @@ -171,3 +177,52 @@ pub fn joined_device_str(sbs: &Vec<(PathBuf, bch_sb_handle)>) -> String { .collect::<Vec<_>>() .join(":") } + +pub fn scan_devices(device: &String, opts: &bch_opts) -> Result<String> { + let mut sbs = scan_sbs(device, opts)?; + + for sb in &mut sbs { + unsafe { + bch_bindgen::sb_io::bch2_free_super(&mut sb.1); + } + } + + Ok(joined_device_str(&sbs)) +} + +#[no_mangle] +pub extern "C" fn bch2_scan_device_sbs(device: *const c_char, ret: *mut sb_names) -> c_int { + let device = unsafe { CStr::from_ptr(device) }; + let device = device.to_str().unwrap().to_string(); + + // 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 mut sbs = sbs.iter() + .map(|(name, sb)| sb_name { + name: CString::new(name.clone().into_os_string().into_string().unwrap()).unwrap().into_raw(), + sb: *sb } ) + .collect::<Vec<sb_name>>(); + + unsafe { + (*ret).data = sbs.as_mut_ptr(); + (*ret).nr = sbs.len(); + (*ret).size = sbs.capacity(); + + std::mem::forget(sbs); + } + 0 +} + +#[no_mangle] +pub extern "C" fn bch2_scan_devices(device: *const c_char) -> *mut c_char { + let device = unsafe { CStr::from_ptr(device) }; + let device = device.to_str().unwrap().to_string(); + + // 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() +} diff --git a/src/rust_to_c.h b/src/rust_to_c.h new file mode 100644 index 00000000..b64059c4 --- /dev/null +++ b/src/rust_to_c.h @@ -0,0 +1,17 @@ +#ifndef _BCACHEFS_TOOLS_RUST_TO_C_H +#define _BCACHEFS_TOOLS_RUST_TO_C_H + +#include "libbcachefs/super_types.h" +#include "libbcachefs/darray.h" + +struct sb_name { + const char *name; + struct bch_sb_handle sb; +}; +typedef DARRAY(struct sb_name) sb_names; + +int bch2_scan_device_sbs(char *, sb_names *ret); + +char *bch2_scan_devices(char *); + +#endif /* _BCACHEFS_TOOLS_RUST_TO_C_H */ |