summaryrefslogtreecommitdiff
path: root/src/device_scan.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/device_scan.rs')
-rw-r--r--src/device_scan.rs57
1 files changed, 56 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()
+}