]> git.sesse.net Git - bcachefs-tools-debian/blob - rust-src/bch_bindgen/src/fs.rs
1176846d90e4d0a0034567a3e8da87d43abcc6f4
[bcachefs-tools-debian] / rust-src / bch_bindgen / src / fs.rs
1 use std::ffi::CString;
2 use std::os::unix::ffi::OsStrExt;
3 use std::path::PathBuf;
4 use crate::c;
5 use crate::errcode::{bch_errcode, errptr_to_result};
6
7 pub struct Fs {
8     pub raw: *mut c::bch_fs,
9 }
10
11 impl Fs {
12     pub fn open(devices: &Vec<PathBuf>, opts: c::bch_opts) -> Result<Fs, bch_errcode> {
13         let devices: Vec<_> = devices.iter()
14             .map(|i| CString::new(i.as_os_str().as_bytes()).unwrap()).collect();
15         let dev_c_strs: Vec<_> = devices.iter()
16             .map(|i| { let p: *const i8 = i.as_ptr(); p })
17             .collect();
18         let dev_c_strarray: *const *mut i8 = dev_c_strs[..].as_ptr() as *const *mut i8;
19
20         let ret = unsafe { c::bch2_fs_open(dev_c_strarray, dev_c_strs.len() as u32, opts) };
21
22         errptr_to_result(ret).map(|fs| Fs { raw: fs})
23     }
24 }
25
26 impl Drop for Fs {
27     fn drop(&mut self) {
28         unsafe { c::bch2_fs_stop(self.raw) }
29     }             
30 }