use crate::bytes::{get_u8, get_u16, get_u64, put_u8, put_u16, put_u64};
use super::{DiskKey, Packed, ParseError};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum DirEntryType {
Unknown,
RegFile,
Dir,
CharDev,
BlockDev,
Fifo,
Socket,
Symlink,
Xattr,
}
impl DirEntryType {
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
DirEntryType::Unknown => 0,
DirEntryType::RegFile => 1,
DirEntryType::Dir => 2,
DirEntryType::CharDev => 3,
DirEntryType::BlockDev => 4,
DirEntryType::Fifo => 5,
DirEntryType::Socket => 6,
DirEntryType::Symlink => 7,
DirEntryType::Xattr => 8,
}
}
#[must_use]
pub const fn from_u8(value: u8) -> Self {
match value {
1 => DirEntryType::RegFile,
2 => DirEntryType::Dir,
3 => DirEntryType::CharDev,
4 => DirEntryType::BlockDev,
5 => DirEntryType::Fifo,
6 => DirEntryType::Socket,
7 => DirEntryType::Symlink,
8 => DirEntryType::Xattr,
_ => DirEntryType::Unknown,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct DirItem {
pub location: DiskKey,
pub transid: u64,
pub data_len: u16,
pub name_len: u16,
pub kind: DirEntryType,
}
impl DirItem {
pub const SIZE: usize = 30;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "btrfs_dir_item",
need: Self::SIZE,
got: buf.len(),
});
}
Ok(Self {
location: DiskKey::read_from(buf)?,
transid: get_u64(buf, 17),
data_len: get_u16(buf, 25),
name_len: get_u16(buf, 27),
kind: DirEntryType::from_u8(get_u8(buf, 29)),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"a directory item needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
self.location.write_to(buf);
put_u64(buf, 17, self.transid);
put_u16(buf, 25, self.data_len);
put_u16(buf, 27, self.name_len);
put_u8(buf, 29, self.kind.to_u8());
}
#[must_use]
pub fn split<'a>(&self, tail: &'a [u8]) -> (&'a [u8], &'a [u8]) {
let name = self.name_len as usize;
(&tail[..name], &tail[name..])
}
}
impl Packed for DirItem {
const STRUCTURE: &'static str = "btrfs_dir_item";
const HEAD: usize = Self::SIZE;
fn read_head(buf: &[u8]) -> Result<Self, ParseError> {
Self::read_from(buf)
}
fn encoded_len(&self) -> usize {
Self::SIZE + self.name_len as usize + self.data_len as usize
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct RootRef {
pub dirid: u64,
pub sequence: u64,
pub name_len: u16,
}
impl RootRef {
pub const SIZE: usize = 18;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "btrfs_root_ref",
need: Self::SIZE,
got: buf.len(),
});
}
Ok(Self {
dirid: get_u64(buf, 0),
sequence: get_u64(buf, 8),
name_len: get_u16(buf, 16),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"a root ref needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
put_u64(buf, 0, self.dirid);
put_u64(buf, 8, self.sequence);
put_u16(buf, 16, self.name_len);
}
}
impl Packed for RootRef {
const STRUCTURE: &'static str = "btrfs_root_ref";
const HEAD: usize = Self::SIZE;
fn read_head(buf: &[u8]) -> Result<Self, ParseError> {
Self::read_from(buf)
}
fn encoded_len(&self) -> usize {
Self::SIZE + self.name_len as usize
}
}
#[cfg(test)]
mod tests {
use super::super::{ItemType, for_each_packed, objectid};
use super::*;
fn record(location: DiskKey, kind: DirEntryType, name: &[u8], value: &[u8]) -> Vec<u8> {
let head = DirItem {
location,
transid: 8,
data_len: value.len() as u16,
name_len: name.len() as u16,
kind,
};
let mut out = vec![0u8; DirItem::SIZE];
head.write_to(&mut out);
out.extend_from_slice(name);
out.extend_from_slice(value);
out
}
#[test]
fn a_directory_entry_round_trips_through_its_thirty_byte_head() {
let head = DirItem {
location: DiskKey::new(257, ItemType::INODE_ITEM, 0),
transid: 8,
data_len: 0,
name_len: 5,
kind: DirEntryType::RegFile,
};
let mut buf = [0u8; DirItem::SIZE];
head.write_to(&mut buf);
assert_eq!(DirItem::read_from(&buf).expect("a full head"), head);
assert_eq!(&buf[0..8], &257u64.to_le_bytes(), "the location's objectid");
assert_eq!(buf[8], 1, "the location's type");
assert_eq!(&buf[25..27], &0u16.to_le_bytes(), "data_len");
assert_eq!(&buf[27..29], &5u16.to_le_bytes(), "name_len");
assert_eq!(buf[29], 1, "a regular file");
}
#[test]
fn two_names_whose_hash_collides_are_two_records_in_one_item() {
let mut data = record(
DiskKey::new(257, ItemType::INODE_ITEM, 0),
DirEntryType::RegFile,
b"one",
b"",
);
data.extend(record(
DiskKey::new(258, ItemType::INODE_ITEM, 0),
DirEntryType::Dir,
b"two",
b"",
));
let mut found = Vec::new();
for_each_packed::<DirItem, _>(&data, |head, tail| {
let (name, value) = head.split(tail);
found.push((
head.location.objectid,
head.kind,
name.to_vec(),
value.len(),
));
true
})
.expect("two entries");
assert_eq!(
found,
vec![
(257, DirEntryType::RegFile, b"one".to_vec(), 0),
(258, DirEntryType::Dir, b"two".to_vec(), 0),
]
);
}
#[test]
fn an_extended_attribute_is_the_same_record_with_its_value_behind_the_name() {
let data = record(
DiskKey::new(0, ItemType::from_value(0), 0),
DirEntryType::Xattr,
b"user.comment",
b"hello",
);
let mut seen = None;
for_each_packed::<DirItem, _>(&data, |head, tail| {
let (name, value) = head.split(tail);
seen = Some((head.kind, name.to_vec(), value.to_vec()));
true
})
.expect("one attribute");
assert_eq!(
seen,
Some((
DirEntryType::Xattr,
b"user.comment".to_vec(),
b"hello".to_vec()
))
);
}
#[test]
fn an_entry_a_subvolume_is_mounted_at_points_at_a_root_rather_than_an_inode() {
let data = record(
DiskKey::new(256, ItemType::ROOT_ITEM, u64::MAX),
DirEntryType::Dir,
b"snap",
b"",
);
let head = DirItem::read_from(&data).expect("a full head");
assert_eq!(head.location.kind, ItemType::ROOT_ITEM);
assert_eq!(head.location.objectid, objectid::FIRST_FREE);
}
#[test]
fn an_entry_type_the_format_has_not_defined_reads_as_unknown() {
assert_eq!(DirEntryType::from_u8(9), DirEntryType::Unknown);
assert_eq!(DirEntryType::from_u8(0), DirEntryType::Unknown);
for value in 0u8..=8 {
let kind = DirEntryType::from_u8(value);
if kind != DirEntryType::Unknown {
assert_eq!(kind.to_u8(), value);
}
}
}
#[test]
fn a_root_ref_round_trips_and_names_the_directory_the_subvolume_is_in() {
let head = RootRef {
dirid: 256,
sequence: 2,
name_len: 4,
};
let mut data = vec![0u8; RootRef::SIZE];
head.write_to(&mut data);
data.extend_from_slice(b"snap");
assert_eq!(RootRef::read_from(&data).expect("a full head"), head);
assert_eq!(&data[0..8], &256u64.to_le_bytes());
assert_eq!(&data[16..18], &4u16.to_le_bytes());
let mut name = Vec::new();
for_each_packed::<RootRef, _>(&data, |_, tail| {
name = tail.to_vec();
true
})
.expect("one reference");
assert_eq!(name, b"snap");
}
#[test]
fn a_head_shorter_than_the_format_writes_is_refused() {
assert!(matches!(
DirItem::read_from(&[0u8; DirItem::SIZE - 1]),
Err(ParseError::TooShort {
structure: "btrfs_dir_item",
need: 30,
got: 29,
})
));
assert!(matches!(
RootRef::read_from(&[0u8; RootRef::SIZE - 1]),
Err(ParseError::TooShort {
structure: "btrfs_root_ref",
..
})
));
}
}