summaryrefslogtreecommitdiff
path: root/rust-src/bch_bindgen/src/rs.rs
diff options
context:
space:
mode:
authorKayla Firestack <dev@kaylafire.me>2021-10-18 13:27:51 -0400
committerKayla Firestack <dev@kaylafire.me>2021-10-18 16:30:52 -0400
commit1f8fc31ddc1975a1f2e7c2fa8f7f611eab761680 (patch)
tree62eef03a32544a61c45665e6c4b4ea8014fd6fc8 /rust-src/bch_bindgen/src/rs.rs
parent7e97ef59bd30154776417748b6518ec43a3db2b9 (diff)
split mount into a library crate for rust reuse
update makefile to output shared library and rust build fix default.nix to properly get the binary name for `ln`ing - move binary to main.rs add rustfmt and gitignore files move build.rs file into bch_bindgen for reuse between projects add outputs to nix flake and checks add mount.toml to makefile
Diffstat (limited to 'rust-src/bch_bindgen/src/rs.rs')
-rw-r--r--rust-src/bch_bindgen/src/rs.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/rust-src/bch_bindgen/src/rs.rs b/rust-src/bch_bindgen/src/rs.rs
new file mode 100644
index 00000000..4452f0b2
--- /dev/null
+++ b/rust-src/bch_bindgen/src/rs.rs
@@ -0,0 +1,58 @@
+use crate::bcachefs;
+
+pub const SUPERBLOCK_MAGIC: uuid::Uuid = uuid::Uuid::from_u128(
+ 0x_c68573f6_4e1a_45ca_8265_f57f48ba6d81
+);
+
+extern "C" {
+ pub static stdout: *mut libc::FILE;
+}
+
+pub enum ReadSuperErr {
+ Io(std::io::Error),
+}
+
+type RResult<T> = std::io::Result<std::io::Result<T>>;
+
+#[tracing_attributes::instrument(skip(opts))]
+pub fn read_super_opts(path: &std::path::Path, mut opts: bcachefs::bch_opts) -> RResult<bcachefs::bch_sb_handle> {
+ // let devp = camino::Utf8Path::from_path(devp).unwrap();
+
+ use std::os::unix::ffi::OsStrExt;
+ let path = std::ffi::CString::new(path.as_os_str().as_bytes())?;
+
+ let mut sb = std::mem::MaybeUninit::zeroed();
+
+ // use gag::{BufferRedirect};
+ // // Stop libbcachefs from spamming the output
+ // let gag = BufferRedirect::stderr().unwrap();
+ // tracing::trace!("entering libbcachefs");
+
+ let ret = unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
+ tracing::trace!(%ret);
+
+ match -ret {
+ libc::EACCES => Err(std::io::Error::new(
+ std::io::ErrorKind::PermissionDenied,
+ "Access Permission Denied",
+ )),
+ 0 => Ok(Ok(unsafe { sb.assume_init() })),
+ 22 => Ok(Err(std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ "Not a BCacheFS SuperBlock",
+ ))),
+ code => {
+ tracing::debug!(msg = "BCacheFS return error code", ?code);
+ Ok(Err(std::io::Error::new(
+ std::io::ErrorKind::Other,
+ "Failed to Read SuperBlock",
+ )))
+ }
+ }
+}
+
+#[tracing_attributes::instrument]
+pub fn read_super(path: &std::path::Path) -> RResult<bcachefs::bch_sb_handle> {
+ let opts = bcachefs::bch_opts::default(); //unsafe {std::mem::MaybeUninit::zeroed().assume_init()};
+ read_super_opts(path, opts)
+}