13 pub use crate::bcachefs::*;
18 pub const fn spos(inode: u64, offset: u64, snapshot: u32) -> Bpos {
19 Bpos { inode, offset, snapshot }
22 pub const fn pos(inode: u64, offset: u64) -> Bpos {
23 spos(inode, offset, 0)
26 pub const POS_MIN: Bpos = spos(0, 0, 0);
27 pub const POS_MAX: Bpos = spos(u64::MAX, u64::MAX, 0);
28 pub const SPOS_MAX: Bpos = spos(u64::MAX, u64::MAX, u32::MAX);
30 use std::cmp::Ordering;
32 impl PartialEq for Bpos {
33 fn eq(&self, other: &Self) -> bool {
34 self.cmp(other) == Ordering::Equal
40 impl PartialOrd for Bpos {
41 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
47 fn cmp(&self, other: &Self) -> Ordering {
48 let l_inode = self.inode;
49 let r_inode = other.inode;
50 let l_offset = self.offset;
51 let r_offset = other.offset;
52 let l_snapshot = self.snapshot;
53 let r_snapshot = other.snapshot;
56 .then(l_offset.cmp(&r_offset))
57 .then(l_snapshot.cmp(&r_snapshot))
64 impl fmt::Display for c::btree_id {
65 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 let s = unsafe { CStr::from_ptr(*c::bch2_btree_ids.get_unchecked(*self as usize)) };
67 let s = s.to_str().unwrap();
72 use std::str::FromStr;
73 use std::ffi::CString;
75 use std::error::Error;
78 pub struct InvalidBtreeId;
80 impl fmt::Display for InvalidBtreeId {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 write!(f, "invalid btree id")
86 impl Error for InvalidBtreeId {
89 impl FromStr for c::btree_id {
90 type Err = InvalidBtreeId;
92 fn from_str(s: &str) -> Result<Self, Self::Err> {
93 let s = CString::new(s).unwrap();
96 let v = unsafe {c::match_string(c::bch2_btree_ids[..].as_ptr(), (-(1 as isize)) as usize, p)};
98 Ok(unsafe { std::mem::transmute(v) })
106 fn new() -> c::printbuf {
107 let mut buf: c::printbuf = Default::default();
109 buf.set_heap_allocated(true);
114 impl Drop for c::printbuf {
116 unsafe { c::bch2_printbuf_exit(self) }
120 impl fmt::Display for Bpos {
121 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
122 let mut buf = c::printbuf::new();
124 unsafe { c::bch2_bpos_to_text(&mut buf, *self) };
126 let s = unsafe { CStr::from_ptr(buf.buf) };
127 let s = s.to_str().unwrap();
132 impl FromStr for c::bpos {
133 type Err = InvalidBtreeId;
135 fn from_str(s: &str) -> Result<Self, Self::Err> {
148 let mut fields = s.split(':');
149 let ino_str = fields.next().ok_or(InvalidBtreeId)?;
150 let off_str = fields.next().ok_or(InvalidBtreeId)?;
151 let snp_str = fields.next();
153 let ino: u64 = ino_str.parse().map_err(|_| InvalidBtreeId)?;
154 let off: u64 = off_str.parse().map_err(|_| InvalidBtreeId)?;
155 let snp: u32 = snp_str.map(|s| s.parse().ok()).flatten().unwrap_or(0);
157 Ok(c::bpos { inode: ino, offset: off, snapshot: snp })