rust: use rustfmt defaults

follow the kernel style guide, i.e idiomatic rust style.

Signed-off-by: Alexander Fougner <fougner89@gmail.com>
This commit is contained in:
Alexander Fougner 2023-01-16 09:22:49 +01:00
parent bdc290eee0
commit 20aecb42d8
10 changed files with 539 additions and 509 deletions

View File

@ -1,12 +1,14 @@
fn main() { 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 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") let top_dir: PathBuf = std::env::var_os("CARGO_MANIFEST_DIR")
.expect("ENV Var 'CARGO_MANIFEST_DIR' Expected") .expect("ENV Var 'CARGO_MANIFEST_DIR' Expected")
.into(); .into();
let libbcachefs_inc_dir = let libbcachefs_inc_dir = std::env::var("LIBBCACHEFS_INCLUDE")
std::env::var("LIBBCACHEFS_INCLUDE").unwrap_or_else(|_| top_dir.join("libbcachefs").display().to_string()); .unwrap_or_else(|_| top_dir.join("libbcachefs").display().to_string());
let libbcachefs_inc_dir = std::path::Path::new(&libbcachefs_inc_dir); let libbcachefs_inc_dir = std::path::Path::new(&libbcachefs_inc_dir);
println!("{}", libbcachefs_inc_dir.display()); println!("{}", libbcachefs_inc_dir.display());
@ -15,8 +17,17 @@ fn main() {
let _libbcachefs_dir = top_dir.join("libbcachefs").join("libbcachefs"); let _libbcachefs_dir = top_dir.join("libbcachefs").join("libbcachefs");
let bindings = bindgen::builder() let bindings = bindgen::builder()
.header(top_dir.join("src").join("libbcachefs_wrapper.h").display().to_string()) .header(
.clang_arg(format!("-I{}", libbcachefs_inc_dir.join("include").display())) 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(format!("-I{}", libbcachefs_inc_dir.display()))
.clang_arg("-DZSTD_STATIC_LINKING_ONLY") .clang_arg("-DZSTD_STATIC_LINKING_ONLY")
.clang_arg("-DNO_BCACHEFS_FS") .clang_arg("-DNO_BCACHEFS_FS")
@ -25,7 +36,9 @@ fn main() {
.derive_default(true) .derive_default(true)
.derive_eq(true) .derive_eq(true)
.layout_tests(true) .layout_tests(true)
.default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: true }) .default_enum_style(bindgen::EnumVariation::Rust {
non_exhaustive: true,
})
.allowlist_function(".*bch2_.*") .allowlist_function(".*bch2_.*")
.allowlist_function("bio_.*") .allowlist_function("bio_.*")
.allowlist_function("bch2_super_write_fd") .allowlist_function("bch2_super_write_fd")
@ -57,8 +70,19 @@ fn main() {
let keyutils = pkg_config::probe_library("libkeyutils").expect("Failed to find keyutils lib"); let keyutils = pkg_config::probe_library("libkeyutils").expect("Failed to find keyutils lib");
let bindings = bindgen::builder() let bindings = bindgen::builder()
.header(top_dir.join("src").join("keyutils_wrapper.h").display().to_string()) .header(
.clang_args(keyutils.include_paths.iter().map(|p| format!("-I{}", p.display()))) top_dir
.join("src")
.join("keyutils_wrapper.h")
.display()
.to_string(),
)
.clang_args(
keyutils
.include_paths
.iter()
.map(|p| format!("-I{}", p.display())),
)
.generate() .generate()
.expect("BindGen Generation Failiure: [Keyutils]"); .expect("BindGen Generation Failiure: [Keyutils]");
bindings bindings

View File

@ -1,2 +1,3 @@
max_width=120 # Default settings, i.e. idiomatic rust
hard_tabs = true edition = "2021"
newline_style = "Unix"

View File

@ -43,7 +43,10 @@ impl PartialEq for bch_sb {
impl bch_sb { impl bch_sb {
pub fn crypt(&self) -> Option<&bch_sb_field_crypt> { pub fn crypt(&self) -> Option<&bch_sb_field_crypt> {
unsafe { unsafe {
let ptr = bch2_sb_field_get(self as *const _ as *mut _, bch_sb_field_type::BCH_SB_FIELD_crypt) as *const u8; 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() { if ptr.is_null() {
None None
} else { } else {

View File

@ -1,8 +1,7 @@
use crate::bcachefs; use crate::bcachefs;
pub const SUPERBLOCK_MAGIC: uuid::Uuid = uuid::Uuid::from_u128( pub const SUPERBLOCK_MAGIC: uuid::Uuid =
0x_c68573f6_4e1a_45ca_8265_f57f48ba6d81 uuid::Uuid::from_u128(0x_c68573f6_4e1a_45ca_8265_f57f48ba6d81);
);
extern "C" { extern "C" {
pub static stdout: *mut libc::FILE; pub static stdout: *mut libc::FILE;
@ -15,7 +14,10 @@ pub enum ReadSuperErr {
type RResult<T> = std::io::Result<std::io::Result<T>>; type RResult<T> = std::io::Result<std::io::Result<T>>;
#[tracing_attributes::instrument(skip(opts))] #[tracing_attributes::instrument(skip(opts))]
pub fn read_super_opts(path: &std::path::Path, mut opts: bcachefs::bch_opts) -> RResult<bcachefs::bch_sb_handle> { 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(); // let devp = camino::Utf8Path::from_path(devp).unwrap();
use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt;
@ -28,7 +30,8 @@ pub fn read_super_opts(path: &std::path::Path, mut opts: bcachefs::bch_opts) ->
// let gag = BufferRedirect::stderr().unwrap(); // let gag = BufferRedirect::stderr().unwrap();
// tracing::trace!("entering libbcachefs"); // tracing::trace!("entering libbcachefs");
let ret = unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) }; let ret =
unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
tracing::trace!(%ret); tracing::trace!(%ret);
match -ret { match -ret {

View File

@ -1,2 +1,3 @@
max_width=120 # Default settings, i.e. idiomatic rust
hard_tabs = true edition = "2021"
newline_style = "Unix"

View File

@ -99,7 +99,8 @@ fn mount_inner(
}); });
let fstype = fstype.as_c_str().to_bytes_with_nul().as_ptr() as *const c_char; let fstype = fstype.as_c_str().to_bytes_with_nul().as_ptr() as *const c_char;
let ret = {let _entered = tracing::info_span!("libc::mount").entered(); let ret = {
let _entered = tracing::info_span!("libc::mount").entered();
tracing::info!("mounting filesystem"); tracing::info!("mounting filesystem");
// REQUIRES: CAP_SYS_ADMIN // REQUIRES: CAP_SYS_ADMIN
unsafe { libc::mount(src, target, fstype, mountflags, data) } unsafe { libc::mount(src, target, fstype, mountflags, data) }
@ -167,14 +168,13 @@ pub fn probe_filesystems() -> anyhow::Result<HashMap<Uuid, FileSystem>> {
udev.match_subsystem("block")?; // find kernel block devices udev.match_subsystem("block")?; // find kernel block devices
let mut fs_map = HashMap::new(); let mut fs_map = HashMap::new();
let devresults = let devresults = udev
udev.scan_devices()? .scan_devices()?
.into_iter() .into_iter()
.filter_map(|dev| dev.devnode().map(ToOwned::to_owned)); .filter_map(|dev| dev.devnode().map(ToOwned::to_owned));
for pathbuf in devresults { for pathbuf in devresults {
match get_super_block_uuid(&pathbuf)? { match get_super_block_uuid(&pathbuf)? {
Ok((uuid_key, superblock)) => { Ok((uuid_key, superblock)) => {
let fs = fs_map.entry(uuid_key).or_insert_with(|| { let fs = fs_map.entry(uuid_key).or_insert_with(|| {
tracing::info!(msg="found bcachefs pool", uuid=?uuid_key); tracing::info!(msg="found bcachefs pool", uuid=?uuid_key);
@ -182,26 +182,31 @@ pub fn probe_filesystems() -> anyhow::Result<HashMap<Uuid, FileSystem>> {
}); });
fs.devices.push(pathbuf); fs.devices.push(pathbuf);
},
Err(e) => { tracing::debug!(inner2_error=?e);}
}
} }
Err(e) => {
tracing::debug!(inner2_error=?e);
}
}
}
tracing::info!(msg = "found filesystems", count = fs_map.len()); tracing::info!(msg = "found filesystems", count = fs_map.len());
Ok(fs_map) Ok(fs_map)
} }
// #[tracing_attributes::instrument(skip(dev, fs_map))] // #[tracing_attributes::instrument(skip(dev, fs_map))]
fn get_super_block_uuid(path: &std::path::Path) -> std::io::Result<std::io::Result<(Uuid, bcachefs::bch_sb_handle)>> { fn get_super_block_uuid(
use gag::{BufferRedirect}; path: &std::path::Path,
) -> std::io::Result<std::io::Result<(Uuid, bcachefs::bch_sb_handle)>> {
use gag::BufferRedirect;
// Stop libbcachefs from spamming the output // Stop libbcachefs from spamming the output
let gag = BufferRedirect::stdout().unwrap(); let gag = BufferRedirect::stdout().unwrap();
let sb = bch_bindgen::rs::read_super(&path)?; let sb = bch_bindgen::rs::read_super(&path)?;
let super_block = match sb { let super_block = match sb {
Err(e) => { return Ok(Err(e)); } Err(e) => {
return Ok(Err(e));
}
Ok(sb) => sb, Ok(sb) => sb,
}; };
drop(gag); drop(gag);

View File

@ -31,8 +31,8 @@ const BCH_KEY_MAGIC: &str = "bch**key";
use crate::filesystem::FileSystem; use crate::filesystem::FileSystem;
fn ask_for_key(fs: &FileSystem) -> anyhow::Result<()> { fn ask_for_key(fs: &FileSystem) -> anyhow::Result<()> {
use anyhow::anyhow; use anyhow::anyhow;
use byteorder::{LittleEndian, ReadBytesExt};
use bch_bindgen::bcachefs::{self, bch2_chacha_encrypt_key, bch_encrypted_key, bch_key}; use bch_bindgen::bcachefs::{self, bch2_chacha_encrypt_key, bch_encrypted_key, bch_key};
use byteorder::{LittleEndian, ReadBytesExt};
use std::os::raw::c_char; use std::os::raw::c_char;
let key_name = std::ffi::CString::new(format!("bcachefs:{}", fs.uuid())).unwrap(); let key_name = std::ffi::CString::new(format!("bcachefs:{}", fs.uuid())).unwrap();

View File

@ -4,11 +4,11 @@ use uuid::Uuid;
pub mod err { pub mod err {
pub enum GError { pub enum GError {
Unknown{ Unknown {
message: std::borrow::Cow<'static, String> message: std::borrow::Cow<'static, String>,
},
} }
} pub type GResult<T, E, OE> = ::core::result::Result<::core::result::Result<T, E>, OE>;
pub type GResult<T, E, OE> =::core::result::Result< ::core::result::Result<T, E>, OE>;
pub type Result<T, E> = GResult<T, E, GError>; pub type Result<T, E> = GResult<T, E, GError>;
} }

View File

@ -1,4 +1,3 @@
use clap::Parser; use clap::Parser;
fn main() { fn main() {
@ -12,17 +11,11 @@ fn main() {
} }
} }
#[tracing_attributes::instrument("main")] #[tracing_attributes::instrument("main")]
pub fn main_inner() -> anyhow::Result<()> { pub fn main_inner() -> anyhow::Result<()> {
use bcachefs_mount::{Cli, filesystem, key}; use bcachefs_mount::{filesystem, key, Cli};
unsafe { unsafe {
libc::setvbuf( libc::setvbuf(filesystem::stdout, std::ptr::null_mut(), libc::_IONBF, 0);
filesystem::stdout,
std::ptr::null_mut(),
libc::_IONBF,
0,
);
// libc::fflush(filesystem::stdout); // libc::fflush(filesystem::stdout);
} }