mirror of
https://github.com/koverstreet/bcachefs-tools.git
synced 2025-02-23 00:00:02 +03:00
rust-src: Clean up read_super bindings
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
parent
f2ab6e552d
commit
c67ea7b6a1
@ -52,6 +52,8 @@ fn main() {
|
|||||||
.allowlist_type("bch_sb_field_.*")
|
.allowlist_type("bch_sb_field_.*")
|
||||||
.allowlist_type("bch_encrypted_key")
|
.allowlist_type("bch_encrypted_key")
|
||||||
.allowlist_type("nonce")
|
.allowlist_type("nonce")
|
||||||
|
.allowlist_type("bch_errcode")
|
||||||
|
.allowlist_function("bch2_err_str")
|
||||||
.newtype_enum("bch_kdf_types")
|
.newtype_enum("bch_kdf_types")
|
||||||
.opaque_type("gendisk")
|
.opaque_type("gendisk")
|
||||||
.opaque_type("bkey")
|
.opaque_type("bkey")
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "../libbcachefs/super-io.h"
|
#include "../libbcachefs/super-io.h"
|
||||||
#include "../libbcachefs/checksum.h"
|
#include "../libbcachefs/checksum.h"
|
||||||
#include "../libbcachefs/bcachefs_format.h"
|
#include "../libbcachefs/bcachefs_format.h"
|
||||||
|
#include "../libbcachefs/errcode.h"
|
||||||
#include "../libbcachefs/opts.h"
|
#include "../libbcachefs/opts.h"
|
||||||
#include "../libbcachefs.h"
|
#include "../libbcachefs.h"
|
||||||
#include "../crypto.h"
|
#include "../crypto.h"
|
||||||
|
@ -1,57 +1,37 @@
|
|||||||
|
use anyhow::anyhow;
|
||||||
use crate::bcachefs;
|
use crate::bcachefs;
|
||||||
use crate::{error, info};
|
use crate::bcachefs::*;
|
||||||
use colored::Colorize;
|
use std::ffi::CStr;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
pub const SUPERBLOCK_MAGIC: uuid::Uuid =
|
impl fmt::Display for bch_errcode {
|
||||||
uuid::Uuid::from_u128(0x_c68573f6_4e1a_45ca_8265_f57f48ba6d81);
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let s = unsafe { CStr::from_ptr(bch2_err_str(*self as i32)) };
|
||||||
extern "C" {
|
write!(f, "{:?}", s)
|
||||||
pub static stdout: *mut libc::FILE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ReadSuperErr {
|
|
||||||
Io(std::io::Error),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RResult<T> = std::io::Result<std::io::Result<T>>;
|
|
||||||
|
|
||||||
pub fn read_super_opts(
|
pub fn read_super_opts(
|
||||||
path: &std::path::Path,
|
path: &std::path::Path,
|
||||||
mut opts: bcachefs::bch_opts,
|
mut opts: bch_opts,
|
||||||
) -> RResult<bcachefs::bch_sb_handle> {
|
) -> anyhow::Result<bch_sb_handle> {
|
||||||
use std::os::unix::ffi::OsStrExt;
|
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 mut sb = std::mem::MaybeUninit::zeroed();
|
||||||
|
|
||||||
let ret =
|
let ret =
|
||||||
unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
|
unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
|
||||||
println!("{}", ret);
|
|
||||||
|
|
||||||
info!("something");
|
if ret != 0 {
|
||||||
error!("an error");
|
let err: bch_errcode = unsafe { ::std::mem::transmute(ret) };
|
||||||
String::from("something").bright_black();
|
Err(anyhow!(err))
|
||||||
match -ret {
|
} else {
|
||||||
libc::EACCES => Err(std::io::Error::new(
|
Ok(unsafe { sb.assume_init() })
|
||||||
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",
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_super(path: &std::path::Path) -> RResult<bcachefs::bch_sb_handle> {
|
pub fn read_super(path: &std::path::Path) -> anyhow::Result<bch_sb_handle> {
|
||||||
let opts = bcachefs::bch_opts::default(); //unsafe {std::mem::MaybeUninit::zeroed().assume_init()};
|
let opts = bcachefs::bch_opts::default();
|
||||||
read_super_opts(path, opts)
|
read_super_opts(path, opts)
|
||||||
}
|
}
|
||||||
|
@ -101,11 +101,11 @@ fn mount(
|
|||||||
mount_inner(device, target, "bcachefs", mountflags, data)
|
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
|
// Stop libbcachefs from spamming the output
|
||||||
let _gag = gag::BufferRedirect::stdout().unwrap();
|
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)>> {
|
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(':') {
|
for dev in opt.dev.split(':') {
|
||||||
let dev = PathBuf::from(dev);
|
let dev = PathBuf::from(dev);
|
||||||
sbs.push(bch_bindgen::rs::read_super(&dev)??);
|
sbs.push(bch_bindgen::rs::read_super(&dev)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
(opt.dev, sbs)
|
(opt.dev, sbs)
|
||||||
|
Loading…
Reference in New Issue
Block a user