diff options
author | Alexander Fougner <fougner89@gmail.com> | 2023-01-16 09:22:49 +0100 |
---|---|---|
committer | Alexander Fougner <fougner89@gmail.com> | 2023-01-16 09:24:01 +0100 |
commit | 20aecb42d8d1c869b17b38b979b9f2f7cbd2ca34 (patch) | |
tree | 7263914b85c3131da345bff192c85224b563d7ac /rust-src/bch_bindgen | |
parent | bdc290eee061d1989059f5a4be10b87c0348bf6f (diff) |
rust: use rustfmt defaults
follow the kernel style guide, i.e idiomatic rust style.
Signed-off-by: Alexander Fougner <fougner89@gmail.com>
Diffstat (limited to 'rust-src/bch_bindgen')
-rw-r--r-- | rust-src/bch_bindgen/build.rs | 146 | ||||
-rw-r--r-- | rust-src/bch_bindgen/rustfmt.toml | 5 | ||||
-rw-r--r-- | rust-src/bch_bindgen/src/bcachefs.rs | 133 | ||||
-rw-r--r-- | rust-src/bch_bindgen/src/lib.rs | 2 | ||||
-rw-r--r-- | rust-src/bch_bindgen/src/rs.rs | 87 |
5 files changed, 202 insertions, 171 deletions
diff --git a/rust-src/bch_bindgen/build.rs b/rust-src/bch_bindgen/build.rs index e729bcab..da1541b1 100644 --- a/rust-src/bch_bindgen/build.rs +++ b/rust-src/bch_bindgen/build.rs @@ -1,67 +1,91 @@ fn main() { - use std::path::PathBuf; + use std::path::PathBuf; - let out_dir: PathBuf = std::env::var_os("OUT_DIR").expect("ENV Var 'OUT_DIR' Expected").into(); - let top_dir: PathBuf = std::env::var_os("CARGO_MANIFEST_DIR") - .expect("ENV Var 'CARGO_MANIFEST_DIR' Expected") - .into(); - let libbcachefs_inc_dir = - std::env::var("LIBBCACHEFS_INCLUDE").unwrap_or_else(|_| top_dir.join("libbcachefs").display().to_string()); - let libbcachefs_inc_dir = std::path::Path::new(&libbcachefs_inc_dir); - println!("{}", libbcachefs_inc_dir.display()); + let out_dir: PathBuf = std::env::var_os("OUT_DIR") + .expect("ENV Var 'OUT_DIR' Expected") + .into(); + let top_dir: PathBuf = std::env::var_os("CARGO_MANIFEST_DIR") + .expect("ENV Var 'CARGO_MANIFEST_DIR' Expected") + .into(); + let libbcachefs_inc_dir = std::env::var("LIBBCACHEFS_INCLUDE") + .unwrap_or_else(|_| top_dir.join("libbcachefs").display().to_string()); + let libbcachefs_inc_dir = std::path::Path::new(&libbcachefs_inc_dir); + println!("{}", libbcachefs_inc_dir.display()); - println!("cargo:rustc-link-lib=dylib=bcachefs"); - println!("cargo:rustc-link-search={}", env!("LIBBCACHEFS_LIB")); + println!("cargo:rustc-link-lib=dylib=bcachefs"); + println!("cargo:rustc-link-search={}", env!("LIBBCACHEFS_LIB")); - let _libbcachefs_dir = top_dir.join("libbcachefs").join("libbcachefs"); - let bindings = bindgen::builder() - .header(top_dir.join("src").join("libbcachefs_wrapper.h").display().to_string()) - .clang_arg(format!("-I{}", libbcachefs_inc_dir.join("include").display())) - .clang_arg(format!("-I{}", libbcachefs_inc_dir.display())) - .clang_arg("-DZSTD_STATIC_LINKING_ONLY") - .clang_arg("-DNO_BCACHEFS_FS") - .clang_arg("-D_GNU_SOURCE") - .derive_debug(true) - .derive_default(true) - .derive_eq(true) - .layout_tests(true) - .default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: true }) - .allowlist_function(".*bch2_.*") - .allowlist_function("bio_.*") - .allowlist_function("bch2_super_write_fd") - .allowlist_function("derive_passphrase") - .allowlist_function("request_key") - .allowlist_function("add_key") - .allowlist_function("keyctl_search") - .blocklist_type("bch_extent_ptr") - .blocklist_type("btree_node") - .blocklist_type("bch_extent_crc32") - .blocklist_type("rhash_lock_head") - .blocklist_type("srcu_struct") - .allowlist_var("BCH_.*") - .allowlist_var("KEY_SPEC_.*") - .allowlist_type("bch_kdf_types") - .allowlist_type("bch_sb_field_.*") - .allowlist_type("bch_encrypted_key") - .allowlist_type("nonce") - .newtype_enum("bch_kdf_types") - .opaque_type("gendisk") - .opaque_type("bkey") - .opaque_type("gc_stripe") - .opaque_type("open_bucket.*") - .generate() - .expect("BindGen Generation Failiure: [libbcachefs_wrapper]"); - bindings - .write_to_file(out_dir.join("bcachefs.rs")) - .expect("Writing to output file failed for: `bcachefs.rs`"); + let _libbcachefs_dir = top_dir.join("libbcachefs").join("libbcachefs"); + let bindings = bindgen::builder() + .header( + top_dir + .join("src") + .join("libbcachefs_wrapper.h") + .display() + .to_string(), + ) + .clang_arg(format!( + "-I{}", + libbcachefs_inc_dir.join("include").display() + )) + .clang_arg(format!("-I{}", libbcachefs_inc_dir.display())) + .clang_arg("-DZSTD_STATIC_LINKING_ONLY") + .clang_arg("-DNO_BCACHEFS_FS") + .clang_arg("-D_GNU_SOURCE") + .derive_debug(true) + .derive_default(true) + .derive_eq(true) + .layout_tests(true) + .default_enum_style(bindgen::EnumVariation::Rust { + non_exhaustive: true, + }) + .allowlist_function(".*bch2_.*") + .allowlist_function("bio_.*") + .allowlist_function("bch2_super_write_fd") + .allowlist_function("derive_passphrase") + .allowlist_function("request_key") + .allowlist_function("add_key") + .allowlist_function("keyctl_search") + .blocklist_type("bch_extent_ptr") + .blocklist_type("btree_node") + .blocklist_type("bch_extent_crc32") + .blocklist_type("rhash_lock_head") + .blocklist_type("srcu_struct") + .allowlist_var("BCH_.*") + .allowlist_var("KEY_SPEC_.*") + .allowlist_type("bch_kdf_types") + .allowlist_type("bch_sb_field_.*") + .allowlist_type("bch_encrypted_key") + .allowlist_type("nonce") + .newtype_enum("bch_kdf_types") + .opaque_type("gendisk") + .opaque_type("bkey") + .opaque_type("gc_stripe") + .opaque_type("open_bucket.*") + .generate() + .expect("BindGen Generation Failiure: [libbcachefs_wrapper]"); + bindings + .write_to_file(out_dir.join("bcachefs.rs")) + .expect("Writing to output file failed for: `bcachefs.rs`"); - let keyutils = pkg_config::probe_library("libkeyutils").expect("Failed to find keyutils lib"); - let bindings = bindgen::builder() - .header(top_dir.join("src").join("keyutils_wrapper.h").display().to_string()) - .clang_args(keyutils.include_paths.iter().map(|p| format!("-I{}", p.display()))) - .generate() - .expect("BindGen Generation Failiure: [Keyutils]"); - bindings - .write_to_file(out_dir.join("keyutils.rs")) - .expect("Writing to output file failed for: `keyutils.rs`"); + let keyutils = pkg_config::probe_library("libkeyutils").expect("Failed to find keyutils lib"); + let bindings = bindgen::builder() + .header( + top_dir + .join("src") + .join("keyutils_wrapper.h") + .display() + .to_string(), + ) + .clang_args( + keyutils + .include_paths + .iter() + .map(|p| format!("-I{}", p.display())), + ) + .generate() + .expect("BindGen Generation Failiure: [Keyutils]"); + bindings + .write_to_file(out_dir.join("keyutils.rs")) + .expect("Writing to output file failed for: `keyutils.rs`"); } diff --git a/rust-src/bch_bindgen/rustfmt.toml b/rust-src/bch_bindgen/rustfmt.toml index a2b7f327..42f2ad7c 100644 --- a/rust-src/bch_bindgen/rustfmt.toml +++ b/rust-src/bch_bindgen/rustfmt.toml @@ -1,2 +1,3 @@ -max_width=120 -hard_tabs = true +# Default settings, i.e. idiomatic rust +edition = "2021" +newline_style = "Unix"
\ No newline at end of file diff --git a/rust-src/bch_bindgen/src/bcachefs.rs b/rust-src/bch_bindgen/src/bcachefs.rs index 6e36790a..fa8dbdeb 100644 --- a/rust-src/bch_bindgen/src/bcachefs.rs +++ b/rust-src/bch_bindgen/src/bcachefs.rs @@ -7,101 +7,104 @@ include!(concat!(env!("OUT_DIR"), "/bcachefs.rs")); use bitfield::bitfield; bitfield! { - pub struct bch_scrypt_flags(u64); - pub N, _: 15, 0; - pub R, _: 31, 16; - pub P, _: 47, 32; + pub struct bch_scrypt_flags(u64); + pub N, _: 15, 0; + pub R, _: 31, 16; + pub P, _: 47, 32; } bitfield! { - pub struct bch_crypt_flags(u64); - pub TYPE, _: 4, 0; + pub struct bch_crypt_flags(u64); + pub TYPE, _: 4, 0; } use memoffset::offset_of; impl bch_sb_field_crypt { - pub fn scrypt_flags(&self) -> Option<bch_scrypt_flags> { - use std::convert::TryInto; - match bch_kdf_types(bch_crypt_flags(self.flags).TYPE().try_into().ok()?) { - bch_kdf_types::BCH_KDF_SCRYPT => Some(bch_scrypt_flags(self.kdf_flags)), - _ => None, - } - } - pub fn key(&self) -> &bch_encrypted_key { - &self.key - } + pub fn scrypt_flags(&self) -> Option<bch_scrypt_flags> { + use std::convert::TryInto; + match bch_kdf_types(bch_crypt_flags(self.flags).TYPE().try_into().ok()?) { + bch_kdf_types::BCH_KDF_SCRYPT => Some(bch_scrypt_flags(self.kdf_flags)), + _ => None, + } + } + pub fn key(&self) -> &bch_encrypted_key { + &self.key + } } impl PartialEq for bch_sb { - fn eq(&self, other: &Self) -> bool { - self.magic.b == other.magic.b - && self.user_uuid.b == other.user_uuid.b - && self.block_size == other.block_size - && self.version == other.version - && self.uuid.b == other.uuid.b - && self.seq == other.seq - } + fn eq(&self, other: &Self) -> bool { + self.magic.b == other.magic.b + && self.user_uuid.b == other.user_uuid.b + && self.block_size == other.block_size + && self.version == other.version + && self.uuid.b == other.uuid.b + && self.seq == other.seq + } } impl bch_sb { - pub fn crypt(&self) -> Option<&bch_sb_field_crypt> { - unsafe { - let ptr = bch2_sb_field_get(self as *const _ as *mut _, bch_sb_field_type::BCH_SB_FIELD_crypt) as *const u8; - if ptr.is_null() { - None - } else { - let offset = offset_of!(bch_sb_field_crypt, field); - Some(&*((ptr.sub(offset)) as *const _)) - } - } - } - pub fn uuid(&self) -> uuid::Uuid { - uuid::Uuid::from_bytes(self.user_uuid.b) - } + pub fn crypt(&self) -> Option<&bch_sb_field_crypt> { + unsafe { + let ptr = bch2_sb_field_get( + self as *const _ as *mut _, + bch_sb_field_type::BCH_SB_FIELD_crypt, + ) as *const u8; + if ptr.is_null() { + None + } else { + let offset = offset_of!(bch_sb_field_crypt, field); + Some(&*((ptr.sub(offset)) as *const _)) + } + } + } + pub fn uuid(&self) -> uuid::Uuid { + uuid::Uuid::from_bytes(self.user_uuid.b) + } - /// Get the nonce used to encrypt the superblock - pub fn nonce(&self) -> nonce { - use byteorder::{LittleEndian, ReadBytesExt}; - let mut internal_uuid = &self.uuid.b[..]; - let dword1 = internal_uuid.read_u32::<LittleEndian>().unwrap(); - let dword2 = internal_uuid.read_u32::<LittleEndian>().unwrap(); - nonce { - d: [0, 0, dword1, dword2], - } - } + /// Get the nonce used to encrypt the superblock + pub fn nonce(&self) -> nonce { + use byteorder::{LittleEndian, ReadBytesExt}; + let mut internal_uuid = &self.uuid.b[..]; + let dword1 = internal_uuid.read_u32::<LittleEndian>().unwrap(); + let dword2 = internal_uuid.read_u32::<LittleEndian>().unwrap(); + nonce { + d: [0, 0, dword1, dword2], + } + } } impl bch_sb_handle { - pub fn sb(&self) -> &bch_sb { - unsafe { &*self.sb } - } + pub fn sb(&self) -> &bch_sb { + unsafe { &*self.sb } + } - pub fn bdev(&self) -> &block_device { - unsafe { &*self.bdev } - } + pub fn bdev(&self) -> &block_device { + unsafe { &*self.bdev } + } } #[repr(C)] // #[repr(align(8))] #[derive(Debug, Default, Copy, Clone)] pub struct bch_extent_ptr { - pub _bitfield_1: __BindgenBitfieldUnit<[u8; 8usize]>, + pub _bitfield_1: __BindgenBitfieldUnit<[u8; 8usize]>, } #[repr(C, packed(8))] pub struct btree_node { - pub csum: bch_csum, - pub magic: __le64, - pub flags: __le64, - pub min_key: bpos, - pub max_key: bpos, - pub _ptr: bch_extent_ptr, - pub format: bkey_format, - pub __bindgen_anon_1: btree_node__bindgen_ty_1, + pub csum: bch_csum, + pub magic: __le64, + pub flags: __le64, + pub min_key: bpos, + pub max_key: bpos, + pub _ptr: bch_extent_ptr, + pub format: bkey_format, + pub __bindgen_anon_1: btree_node__bindgen_ty_1, } #[repr(C, packed(8))] // #[repr(align(8))] #[derive(Debug, Default, Copy, Clone)] pub struct bch_extent_crc32 { - pub _bitfield_1: __BindgenBitfieldUnit<[u8; 4usize]>, - pub csum: __u32, + pub _bitfield_1: __BindgenBitfieldUnit<[u8; 4usize]>, + pub csum: __u32, } // #[repr(u8)] diff --git a/rust-src/bch_bindgen/src/lib.rs b/rust-src/bch_bindgen/src/lib.rs index c19b5a2f..dc8bcf4a 100644 --- a/rust-src/bch_bindgen/src/lib.rs +++ b/rust-src/bch_bindgen/src/lib.rs @@ -3,5 +3,5 @@ pub mod keyutils; pub mod rs; pub mod c { - pub use crate::bcachefs::*; + pub use crate::bcachefs::*; } diff --git a/rust-src/bch_bindgen/src/rs.rs b/rust-src/bch_bindgen/src/rs.rs index 4452f0b2..7a3a3eaf 100644 --- a/rust-src/bch_bindgen/src/rs.rs +++ b/rust-src/bch_bindgen/src/rs.rs @@ -1,58 +1,61 @@ use crate::bcachefs; -pub const SUPERBLOCK_MAGIC: uuid::Uuid = uuid::Uuid::from_u128( - 0x_c68573f6_4e1a_45ca_8265_f57f48ba6d81 -); - +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 static stdout: *mut libc::FILE; } pub enum ReadSuperErr { - Io(std::io::Error), + 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", - ))) - } - } +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) + let opts = bcachefs::bch_opts::default(); //unsafe {std::mem::MaybeUninit::zeroed().assume_init()}; + read_super_opts(path, opts) } |