2021-10-18 20:27:51 +03:00
|
|
|
pub mod bcachefs;
|
2023-02-28 10:11:05 +03:00
|
|
|
pub mod bkey;
|
2024-05-27 03:38:08 +03:00
|
|
|
pub mod btree;
|
2023-02-27 05:38:12 +03:00
|
|
|
pub mod errcode;
|
|
|
|
pub mod fs;
|
2024-05-27 03:38:08 +03:00
|
|
|
pub mod keyutils;
|
2023-02-28 14:15:48 +03:00
|
|
|
pub mod opts;
|
2024-05-27 03:38:08 +03:00
|
|
|
pub mod sb_io;
|
2023-02-28 14:15:48 +03:00
|
|
|
pub use paste::paste;
|
2023-02-27 05:38:12 +03:00
|
|
|
|
2021-10-18 20:27:51 +03:00
|
|
|
pub mod c {
|
2023-01-16 11:22:49 +03:00
|
|
|
pub use crate::bcachefs::*;
|
2021-10-18 20:27:51 +03:00
|
|
|
}
|
2023-02-27 05:38:12 +03:00
|
|
|
|
|
|
|
use c::bpos as Bpos;
|
|
|
|
|
|
|
|
pub const fn spos(inode: u64, offset: u64, snapshot: u32) -> Bpos {
|
2024-05-27 03:38:08 +03:00
|
|
|
Bpos {
|
|
|
|
inode,
|
|
|
|
offset,
|
|
|
|
snapshot,
|
|
|
|
}
|
2023-02-27 05:38:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn pos(inode: u64, offset: u64) -> Bpos {
|
|
|
|
spos(inode, offset, 0)
|
|
|
|
}
|
|
|
|
|
2024-05-27 03:38:08 +03:00
|
|
|
pub const POS_MIN: Bpos = spos(0, 0, 0);
|
|
|
|
pub const POS_MAX: Bpos = spos(u64::MAX, u64::MAX, 0);
|
2023-02-27 05:38:12 +03:00
|
|
|
pub const SPOS_MAX: Bpos = spos(u64::MAX, u64::MAX, u32::MAX);
|
|
|
|
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
|
|
|
impl PartialEq for Bpos {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.cmp(other) == Ordering::Equal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Bpos {}
|
|
|
|
|
|
|
|
impl PartialOrd for Bpos {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Bpos {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
2024-05-27 03:38:08 +03:00
|
|
|
let l_inode = self.inode;
|
|
|
|
let r_inode = other.inode;
|
|
|
|
let l_offset = self.offset;
|
|
|
|
let r_offset = other.offset;
|
|
|
|
let l_snapshot = self.snapshot;
|
|
|
|
let r_snapshot = other.snapshot;
|
|
|
|
|
|
|
|
l_inode
|
|
|
|
.cmp(&r_inode)
|
2023-02-27 05:38:12 +03:00
|
|
|
.then(l_offset.cmp(&r_offset))
|
|
|
|
.then(l_snapshot.cmp(&r_snapshot))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
impl fmt::Display for c::btree_id {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2023-10-20 05:52:02 +03:00
|
|
|
let s = unsafe { CStr::from_ptr(c::bch2_btree_id_str(*self)) };
|
2023-02-27 05:38:12 +03:00
|
|
|
let s = s.to_str().unwrap();
|
|
|
|
write!(f, "{}", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::ffi::CString;
|
2024-05-27 03:38:08 +03:00
|
|
|
use std::str::FromStr;
|
|
|
|
use std::{os::unix::ffi::OsStrExt, path::Path};
|
2024-03-03 02:41:55 +03:00
|
|
|
|
|
|
|
pub fn path_to_cstr<P: AsRef<Path>>(p: P) -> CString {
|
|
|
|
CString::new(p.as_ref().as_os_str().as_bytes()).unwrap()
|
|
|
|
}
|
2023-02-27 05:38:12 +03:00
|
|
|
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2024-05-10 00:14:06 +03:00
|
|
|
pub enum BchToolsErr {
|
|
|
|
InvalidBtreeId,
|
|
|
|
InvalidBkeyType,
|
|
|
|
InvalidBpos,
|
|
|
|
}
|
2023-02-27 05:38:12 +03:00
|
|
|
|
2024-05-10 00:14:06 +03:00
|
|
|
impl fmt::Display for BchToolsErr {
|
2023-02-27 05:38:12 +03:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2024-05-10 00:14:06 +03:00
|
|
|
match self {
|
|
|
|
BchToolsErr::InvalidBtreeId => write!(f, "invalid btree id"),
|
|
|
|
BchToolsErr::InvalidBkeyType => write!(f, "invalid bkey type"),
|
|
|
|
BchToolsErr::InvalidBpos => write!(f, "invalid bpos"),
|
|
|
|
}
|
2023-02-27 05:38:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 03:38:08 +03:00
|
|
|
impl Error for BchToolsErr {}
|
2023-02-27 05:38:12 +03:00
|
|
|
|
|
|
|
impl FromStr for c::btree_id {
|
2024-05-10 00:14:06 +03:00
|
|
|
type Err = BchToolsErr;
|
2023-02-27 05:38:12 +03:00
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let s = CString::new(s).unwrap();
|
2023-03-04 15:35:29 +03:00
|
|
|
let p = s.as_ptr();
|
2023-02-27 05:38:12 +03:00
|
|
|
|
2024-05-27 03:38:08 +03:00
|
|
|
let v = unsafe {
|
|
|
|
c::match_string(
|
|
|
|
c::__bch2_btree_ids[..].as_ptr(),
|
|
|
|
(-(1 as isize)) as usize,
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
};
|
2023-02-27 05:38:12 +03:00
|
|
|
if v >= 0 {
|
|
|
|
Ok(unsafe { std::mem::transmute(v) })
|
|
|
|
} else {
|
2024-05-10 00:14:06 +03:00
|
|
|
Err(BchToolsErr::InvalidBtreeId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for c::bch_bkey_type {
|
|
|
|
type Err = BchToolsErr;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let s = CString::new(s).unwrap();
|
|
|
|
let p = s.as_ptr();
|
|
|
|
|
2024-05-27 03:38:08 +03:00
|
|
|
let v = unsafe {
|
|
|
|
c::match_string(c::bch2_bkey_types[..].as_ptr(), (-(1 as isize)) as usize, p)
|
|
|
|
};
|
2024-05-10 00:14:06 +03:00
|
|
|
if v >= 0 {
|
|
|
|
Ok(unsafe { std::mem::transmute(v) })
|
|
|
|
} else {
|
|
|
|
Err(BchToolsErr::InvalidBkeyType)
|
2023-02-27 05:38:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl c::printbuf {
|
|
|
|
fn new() -> c::printbuf {
|
|
|
|
let mut buf: c::printbuf = Default::default();
|
|
|
|
|
|
|
|
buf.set_heap_allocated(true);
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for c::printbuf {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { c::bch2_printbuf_exit(self) }
|
2024-05-27 03:38:08 +03:00
|
|
|
}
|
2023-02-27 05:38:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Bpos {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let mut buf = c::printbuf::new();
|
|
|
|
|
|
|
|
unsafe { c::bch2_bpos_to_text(&mut buf, *self) };
|
2024-05-27 03:38:08 +03:00
|
|
|
|
2023-02-27 05:38:12 +03:00
|
|
|
let s = unsafe { CStr::from_ptr(buf.buf) };
|
|
|
|
let s = s.to_str().unwrap();
|
|
|
|
write!(f, "{}", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for c::bpos {
|
2024-05-10 00:14:06 +03:00
|
|
|
type Err = BchToolsErr;
|
2023-02-27 05:38:12 +03:00
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
if s == "POS_MIN" {
|
2023-02-28 08:33:42 +03:00
|
|
|
return Ok(POS_MIN);
|
2023-02-27 05:38:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if s == "POS_MAX" {
|
2023-02-28 08:33:42 +03:00
|
|
|
return Ok(POS_MAX);
|
2023-02-27 05:38:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if s == "SPOS_MAX" {
|
2023-02-28 08:33:42 +03:00
|
|
|
return Ok(SPOS_MAX);
|
2023-02-27 05:38:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut fields = s.split(':');
|
2024-05-10 00:14:06 +03:00
|
|
|
let ino_str = fields.next().ok_or(BchToolsErr::InvalidBpos)?;
|
|
|
|
let off_str = fields.next().ok_or(BchToolsErr::InvalidBpos)?;
|
2023-02-27 05:38:12 +03:00
|
|
|
let snp_str = fields.next();
|
|
|
|
|
2024-05-27 03:38:08 +03:00
|
|
|
let ino: u64 = ino_str.parse().map_err(|_| BchToolsErr::InvalidBpos)?;
|
|
|
|
let off: u64 = off_str.parse().map_err(|_| BchToolsErr::InvalidBpos)?;
|
|
|
|
let snp: u32 = snp_str.map(|s| s.parse().ok()).flatten().unwrap_or(0);
|
2023-02-27 05:38:12 +03:00
|
|
|
|
2024-05-27 03:38:08 +03:00
|
|
|
Ok(c::bpos {
|
|
|
|
inode: ino,
|
|
|
|
offset: off,
|
|
|
|
snapshot: snp,
|
|
|
|
})
|
2023-02-27 05:38:12 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-06 10:21:52 +03:00
|
|
|
|
|
|
|
pub fn printbuf_to_formatter<F>(f: &mut fmt::Formatter<'_>, func: F) -> fmt::Result
|
2024-05-27 03:38:08 +03:00
|
|
|
where
|
|
|
|
F: Fn(*mut c::printbuf),
|
|
|
|
{
|
2023-03-06 10:21:52 +03:00
|
|
|
let mut buf = c::printbuf::new();
|
|
|
|
|
|
|
|
func(&mut buf);
|
|
|
|
|
|
|
|
let s = unsafe { CStr::from_ptr(buf.buf) };
|
2024-01-29 16:33:15 +03:00
|
|
|
f.write_str(&s.to_string_lossy())
|
2023-03-06 10:21:52 +03:00
|
|
|
}
|