use crate::bytes::{get_u8, get_u16, get_u64, put_u8, put_u16, put_u64};
use super::ParseError;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum ExtentKind {
Inline,
Regular,
Prealloc,
Unknown(u8),
}
impl ExtentKind {
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
ExtentKind::Inline => 0,
ExtentKind::Regular => 1,
ExtentKind::Prealloc => 2,
ExtentKind::Unknown(value) => value,
}
}
#[must_use]
pub const fn from_u8(value: u8) -> Self {
match value {
0 => ExtentKind::Inline,
1 => ExtentKind::Regular,
2 => ExtentKind::Prealloc,
other => ExtentKind::Unknown(other),
}
}
#[must_use]
pub const fn is_inline(self) -> bool {
matches!(self, ExtentKind::Inline)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum Compression {
None,
Zlib,
Lzo,
Zstd,
Unknown(u8),
}
impl Compression {
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
Compression::None => 0,
Compression::Zlib => 1,
Compression::Lzo => 2,
Compression::Zstd => 3,
Compression::Unknown(value) => value,
}
}
#[must_use]
pub const fn from_u8(value: u8) -> Self {
match value {
0 => Compression::None,
1 => Compression::Zlib,
2 => Compression::Lzo,
3 => Compression::Zstd,
other => Compression::Unknown(other),
}
}
#[must_use]
pub const fn name(self) -> Option<&'static str> {
Some(match self {
Compression::None => "none",
Compression::Zlib => "zlib",
Compression::Lzo => "lzo",
Compression::Zstd => "zstd",
Compression::Unknown(_) => return None,
})
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct FileExtentItem {
pub generation: u64,
pub ram_bytes: u64,
pub compression: Compression,
pub encryption: u8,
pub other_encoding: u16,
pub kind: ExtentKind,
pub disk_bytenr: u64,
pub disk_num_bytes: u64,
pub offset: u64,
pub num_bytes: u64,
}
impl FileExtentItem {
pub const SIZE: usize = 53;
pub const INLINE_DATA_START: usize = 21;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::INLINE_DATA_START {
return Err(ParseError::TooShort {
structure: "btrfs_file_extent_item",
need: Self::INLINE_DATA_START,
got: buf.len(),
});
}
let kind = ExtentKind::from_u8(get_u8(buf, 20));
let mut item = Self {
generation: get_u64(buf, 0),
ram_bytes: get_u64(buf, 8),
compression: Compression::from_u8(get_u8(buf, 16)),
encryption: get_u8(buf, 17),
other_encoding: get_u16(buf, 18),
kind,
disk_bytenr: 0,
disk_num_bytes: 0,
offset: 0,
num_bytes: 0,
};
if kind.is_inline() {
return Ok(item);
}
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "btrfs_file_extent_item",
need: Self::SIZE,
got: buf.len(),
});
}
item.disk_bytenr = get_u64(buf, 21);
item.disk_num_bytes = get_u64(buf, 29);
item.offset = get_u64(buf, 37);
item.num_bytes = get_u64(buf, 45);
Ok(item)
}
pub fn write_to(&self, buf: &mut [u8]) {
let need = if self.kind.is_inline() {
Self::INLINE_DATA_START
} else {
Self::SIZE
};
assert!(
buf.len() >= need,
"an extent record needs {need} bytes and was given {}",
buf.len()
);
put_u64(buf, 0, self.generation);
put_u64(buf, 8, self.ram_bytes);
put_u8(buf, 16, self.compression.to_u8());
put_u8(buf, 17, self.encryption);
put_u16(buf, 18, self.other_encoding);
put_u8(buf, 20, self.kind.to_u8());
if self.kind.is_inline() {
return;
}
put_u64(buf, 21, self.disk_bytenr);
put_u64(buf, 29, self.disk_num_bytes);
put_u64(buf, 37, self.offset);
put_u64(buf, 45, self.num_bytes);
}
pub fn inline_data<'a>(&self, data: &'a [u8]) -> Result<&'a [u8], ParseError> {
if data.len() < Self::INLINE_DATA_START {
return Err(ParseError::TooShort {
structure: "btrfs_file_extent_item",
need: Self::INLINE_DATA_START,
got: data.len(),
});
}
Ok(&data[Self::INLINE_DATA_START..])
}
#[must_use]
pub const fn is_hole(&self) -> bool {
!self.kind.is_inline() && self.disk_bytenr == 0
}
}
#[cfg(test)]
mod tests {
use super::*;
fn a_regular_extent() -> FileExtentItem {
FileExtentItem {
generation: 8,
ram_bytes: 8192,
compression: Compression::None,
encryption: 0,
other_encoding: 0,
kind: ExtentKind::Regular,
disk_bytenr: 13_631_488,
disk_num_bytes: 8192,
offset: 4096,
num_bytes: 4096,
}
}
#[test]
fn an_addressed_extent_round_trips_through_its_fifty_three_bytes() {
let item = a_regular_extent();
let mut buf = [0u8; FileExtentItem::SIZE];
item.write_to(&mut buf);
assert_eq!(FileExtentItem::read_from(&buf).expect("a full item"), item);
assert_eq!(buf[20], 1, "a regular extent");
assert_eq!(&buf[21..29], &13_631_488u64.to_le_bytes(), "disk_bytenr");
assert_eq!(
&buf[37..45],
&4096u64.to_le_bytes(),
"offset into the extent"
);
assert_eq!(&buf[45..53], &4096u64.to_le_bytes(), "num_bytes");
}
#[test]
fn an_inline_extent_is_a_head_and_the_files_bytes() {
let item = FileExtentItem {
ram_bytes: 5,
kind: ExtentKind::Inline,
..a_regular_extent()
};
let mut data = vec![0u8; FileExtentItem::INLINE_DATA_START];
item.write_to(&mut data);
data.extend_from_slice(b"hello");
let read = FileExtentItem::read_from(&data).expect("an inline record");
assert_eq!(read.kind, ExtentKind::Inline);
assert_eq!(read.inline_data(&data).expect("the bytes"), b"hello");
assert_eq!(read.disk_bytenr, 0);
assert_eq!(read.num_bytes, 0);
let empty = &data[..FileExtentItem::INLINE_DATA_START];
assert_eq!(read.inline_data(empty).expect("no bytes"), b"");
}
#[test]
fn an_inline_record_is_readable_from_an_item_too_short_for_an_addressed_one() {
let item = FileExtentItem {
kind: ExtentKind::Inline,
..a_regular_extent()
};
let mut buf = [0u8; FileExtentItem::INLINE_DATA_START];
item.write_to(&mut buf);
assert!(FileExtentItem::read_from(&buf).is_ok());
let mut truncated = [0u8; FileExtentItem::SIZE - 1];
a_regular_extent().write_to(&mut [0u8; FileExtentItem::SIZE]);
truncated[20] = 1;
assert!(matches!(
FileExtentItem::read_from(&truncated),
Err(ParseError::TooShort {
structure: "btrfs_file_extent_item",
need: 53,
got: 52,
})
));
}
#[test]
fn a_hole_is_an_addressed_record_whose_extent_address_is_zero() {
let hole = FileExtentItem {
disk_bytenr: 0,
disk_num_bytes: 0,
offset: 0,
num_bytes: 4096,
..a_regular_extent()
};
assert!(hole.is_hole());
assert!(!a_regular_extent().is_hole());
let inline = FileExtentItem {
kind: ExtentKind::Inline,
disk_bytenr: 0,
..a_regular_extent()
};
assert!(!inline.is_hole());
}
#[test]
fn a_compressed_extent_is_two_lengths_and_a_named_algorithm() {
let item = FileExtentItem {
compression: Compression::Zstd,
ram_bytes: 1 << 16,
disk_num_bytes: 4096,
num_bytes: 1 << 16,
offset: 0,
..a_regular_extent()
};
let mut buf = [0u8; FileExtentItem::SIZE];
item.write_to(&mut buf);
assert_eq!(buf[16], 3);
let read = FileExtentItem::read_from(&buf).expect("a full item");
assert_eq!(read.compression, Compression::Zstd);
assert_eq!(read.compression.name(), Some("zstd"));
assert!(read.disk_num_bytes < read.ram_bytes);
assert_eq!(Compression::from_u8(9), Compression::Unknown(9));
assert_eq!(Compression::from_u8(9).name(), None);
assert_eq!(Compression::from_u8(9).to_u8(), 9);
}
#[test]
fn an_extent_shape_the_format_has_not_defined_keeps_its_byte() {
assert_eq!(ExtentKind::from_u8(7), ExtentKind::Unknown(7));
assert_eq!(ExtentKind::from_u8(7).to_u8(), 7);
assert!(!ExtentKind::from_u8(7).is_inline());
}
}