2021-10-18 20:27:51 +03:00
|
|
|
use crate::bcachefs;
|
2023-01-16 19:08:52 +03:00
|
|
|
use crate::{error, info};
|
|
|
|
use colored::Colorize;
|
2021-10-18 20:27:51 +03:00
|
|
|
|
2023-01-16 11:22:49 +03:00
|
|
|
pub const SUPERBLOCK_MAGIC: uuid::Uuid =
|
|
|
|
uuid::Uuid::from_u128(0x_c68573f6_4e1a_45ca_8265_f57f48ba6d81);
|
|
|
|
|
2021-10-18 20:27:51 +03:00
|
|
|
extern "C" {
|
2023-01-16 11:22:49 +03:00
|
|
|
pub static stdout: *mut libc::FILE;
|
2021-10-18 20:27:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ReadSuperErr {
|
2023-01-16 11:22:49 +03:00
|
|
|
Io(std::io::Error),
|
2021-10-18 20:27:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type RResult<T> = std::io::Result<std::io::Result<T>>;
|
|
|
|
|
2023-01-16 11:22:49 +03:00
|
|
|
pub fn read_super_opts(
|
|
|
|
path: &std::path::Path,
|
|
|
|
mut opts: bcachefs::bch_opts,
|
|
|
|
) -> RResult<bcachefs::bch_sb_handle> {
|
|
|
|
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();
|
|
|
|
|
|
|
|
let ret =
|
|
|
|
unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
|
2023-01-16 19:08:52 +03:00
|
|
|
println!("{}", ret);
|
2023-01-16 11:22:49 +03:00
|
|
|
|
2023-01-16 19:08:52 +03:00
|
|
|
info!("something");
|
|
|
|
error!("an error");
|
|
|
|
String::from("something").bright_black();
|
2023-01-16 11:22:49 +03:00
|
|
|
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 => {
|
2023-01-16 19:08:52 +03:00
|
|
|
println!("BCacheFS return error code: {}", code);
|
2023-01-16 11:22:49 +03:00
|
|
|
Ok(Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Failed to Read SuperBlock",
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 20:27:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_super(path: &std::path::Path) -> RResult<bcachefs::bch_sb_handle> {
|
2023-01-16 11:22:49 +03:00
|
|
|
let opts = bcachefs::bch_opts::default(); //unsafe {std::mem::MaybeUninit::zeroed().assume_init()};
|
|
|
|
read_super_opts(path, opts)
|
2021-10-18 20:27:51 +03:00
|
|
|
}
|