summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/device_scan.rs39
-rw-r--r--src/rust_to_c.h11
2 files changed, 46 insertions, 4 deletions
diff --git a/src/device_scan.rs b/src/device_scan.rs
index fda2d460..a6b62290 100644
--- a/src/device_scan.rs
+++ b/src/device_scan.rs
@@ -1,5 +1,5 @@
use std::{
- ffi::{CStr, CString, c_char},
+ ffi::{CStr, CString, c_char, c_int},
collections::HashMap,
env,
path::{Path, PathBuf},
@@ -7,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;
@@ -186,12 +191,38 @@ pub fn scan_devices(device: &String, opts: &bch_opts) -> Result<String> {
}
#[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();
- let opts = bch_bindgen::opts::parse_mount_opts(None, None, true)
- .unwrap_or_default();
+ // 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
index 4f23e1dd..b64059c4 100644
--- a/src/rust_to_c.h
+++ b/src/rust_to_c.h
@@ -1,6 +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 */