rust-src: Clean up read_super bindings

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2023-02-21 15:58:42 -05:00
parent f2ab6e552d
commit c67ea7b6a1
4 changed files with 25 additions and 42 deletions

View File

@ -52,6 +52,8 @@ fn main() {
.allowlist_type("bch_sb_field_.*")
.allowlist_type("bch_encrypted_key")
.allowlist_type("nonce")
.allowlist_type("bch_errcode")
.allowlist_function("bch2_err_str")
.newtype_enum("bch_kdf_types")
.opaque_type("gendisk")
.opaque_type("bkey")

View File

@ -1,6 +1,7 @@
#include "../libbcachefs/super-io.h"
#include "../libbcachefs/checksum.h"
#include "../libbcachefs/bcachefs_format.h"
#include "../libbcachefs/errcode.h"
#include "../libbcachefs/opts.h"
#include "../libbcachefs.h"
#include "../crypto.h"

View File

@ -1,57 +1,37 @@
use anyhow::anyhow;
use crate::bcachefs;
use crate::{error, info};
use colored::Colorize;
use crate::bcachefs::*;
use std::ffi::CStr;
use std::fmt;
pub const SUPERBLOCK_MAGIC: uuid::Uuid =
uuid::Uuid::from_u128(0x_c68573f6_4e1a_45ca_8265_f57f48ba6d81);
extern "C" {
pub static stdout: *mut libc::FILE;
impl fmt::Display for bch_errcode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = unsafe { CStr::from_ptr(bch2_err_str(*self as i32)) };
write!(f, "{:?}", s)
}
}
pub enum ReadSuperErr {
Io(std::io::Error),
}
type RResult<T> = std::io::Result<std::io::Result<T>>;
pub fn read_super_opts(
path: &std::path::Path,
mut opts: bcachefs::bch_opts,
) -> RResult<bcachefs::bch_sb_handle> {
mut opts: bch_opts,
) -> anyhow::Result<bch_sb_handle> {
use std::os::unix::ffi::OsStrExt;
let path = std::ffi::CString::new(path.as_os_str().as_bytes())?;
let path = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap();
let mut sb = std::mem::MaybeUninit::zeroed();
let ret =
unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
println!("{}", ret);
info!("something");
error!("an error");
String::from("something").bright_black();
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 => {
println!("BCacheFS return error code: {}", code);
Ok(Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Failed to Read SuperBlock",
)))
}
if ret != 0 {
let err: bch_errcode = unsafe { ::std::mem::transmute(ret) };
Err(anyhow!(err))
} else {
Ok(unsafe { sb.assume_init() })
}
}
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()};
pub fn read_super(path: &std::path::Path) -> anyhow::Result<bch_sb_handle> {
let opts = bcachefs::bch_opts::default();
read_super_opts(path, opts)
}

View File

@ -101,11 +101,11 @@ fn mount(
mount_inner(device, target, "bcachefs", mountflags, data)
}
fn read_super_silent(path: &std::path::PathBuf) -> std::io::Result<bch_sb_handle> {
fn read_super_silent(path: &std::path::PathBuf) -> anyhow::Result<bch_sb_handle> {
// Stop libbcachefs from spamming the output
let _gag = gag::BufferRedirect::stdout().unwrap();
bch_bindgen::rs::read_super(&path)?
bch_bindgen::rs::read_super(&path)
}
fn get_devices_by_uuid(uuid: Uuid) -> anyhow::Result<Vec<(PathBuf, bch_sb_handle)>> {
@ -182,7 +182,7 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> {
for dev in opt.dev.split(':') {
let dev = PathBuf::from(dev);
sbs.push(bch_bindgen::rs::read_super(&dev)??);
sbs.push(bch_bindgen::rs::read_super(&dev)?);
}
(opt.dev, sbs)