use crate::bytes::{be_u16, be_u32, be_u64, u8_at};
use crate::crc::{crc_status, DINODE_CRC_OFF};
use crate::error::XfsError;
pub const XFS_DINODE_MAGIC: u16 = 0x494e;
pub const XFS_DIFLAG2_BIGTIME: u64 = 1 << 3;
const XFS_BIGTIME_EPOCH_OFFSET: i64 = 1 << 31;
const NSEC_PER_SEC: u64 = 1_000_000_000;
const V2_LITINO_OFFSET: usize = 100;
const V3_LITINO_OFFSET: usize = 176;
const V2_MIN_LEN: usize = V2_LITINO_OFFSET;
const V3_MIN_LEN: usize = V3_LITINO_OFFSET;
const S_IFMT: u16 = 0o170_000;
const S_IFIFO: u16 = 0o010_000;
const S_IFCHR: u16 = 0o020_000;
const S_IFDIR: u16 = 0o040_000;
const S_IFBLK: u16 = 0o060_000;
const S_IFREG: u16 = 0o100_000;
const S_IFLNK: u16 = 0o120_000;
const S_IFSOCK: u16 = 0o140_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum InodeFormat {
Dev,
Local,
Extents,
Btree,
Other(u8),
}
impl InodeFormat {
#[must_use]
pub fn from_raw(v: u8) -> Self {
match v {
0 => Self::Dev,
1 => Self::Local,
2 => Self::Extents,
3 => Self::Btree,
other => Self::Other(other),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FileType {
Fifo,
CharDevice,
Directory,
BlockDevice,
Regular,
Symlink,
Socket,
Other(u16),
}
impl FileType {
#[must_use]
pub fn from_mode(mode: u16) -> Self {
match mode & S_IFMT {
S_IFIFO => Self::Fifo,
S_IFCHR => Self::CharDevice,
S_IFDIR => Self::Directory,
S_IFBLK => Self::BlockDevice,
S_IFREG => Self::Regular,
S_IFLNK => Self::Symlink,
S_IFSOCK => Self::Socket,
other => Self::Other(other),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct XfsTimestamp {
pub secs: i64,
pub nsecs: u32,
}
impl XfsTimestamp {
#[must_use]
fn from_legacy(raw: u64) -> Self {
let secs = i64::from((raw >> 32) as i32);
let nsecs = (raw & 0xffff_ffff) as u32;
Self { secs, nsecs }
}
#[must_use]
fn from_bigtime(raw: u64) -> Self {
let ondisk_secs = raw / NSEC_PER_SEC;
let nsecs = (raw % NSEC_PER_SEC) as u32;
let secs = ondisk_secs as i64 - XFS_BIGTIME_EPOCH_OFFSET;
Self { secs, nsecs }
}
#[must_use]
fn decode(raw: u64, bigtime: bool) -> Self {
if bigtime {
Self::from_bigtime(raw)
} else {
Self::from_legacy(raw)
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Inode {
pub magic: u16,
pub mode: u16,
pub version: u8,
pub format: InodeFormat,
pub aformat: u8,
pub size: u64,
pub nblocks: u64,
pub nextents: u32,
pub aextents: u16,
pub forkoff: u8,
pub atime: XfsTimestamp,
pub mtime: XfsTimestamp,
pub ctime: XfsTimestamp,
pub crtime: Option<XfsTimestamp>,
pub flags2: Option<u64>,
pub cowextsize: Option<u32>,
pub di_ino: Option<u64>,
pub uuid: Option<[u8; 16]>,
pub crc: Option<u32>,
pub crc_valid: Option<bool>,
pub data_fork: Vec<u8>,
}
impl Inode {
pub fn parse(data: &[u8]) -> Result<Self, XfsError> {
let magic = be_u16(data, 0);
if magic != XFS_DINODE_MAGIC {
let bytes = [0, 0, u8_at(data, 0), u8_at(data, 1)];
return Err(XfsError::BadMagic {
found: u32::from(magic),
bytes,
});
}
let version = u8_at(data, 4);
let is_v3 = version >= 3;
let need = if is_v3 { V3_MIN_LEN } else { V2_MIN_LEN };
if data.len() < need {
return Err(XfsError::Truncated {
structure: "inode core",
need,
have: data.len(),
});
}
let flags2 = if is_v3 { Some(be_u64(data, 120)) } else { None };
let bigtime = flags2.is_some_and(|f| f & XFS_DIFLAG2_BIGTIME != 0);
let atime = XfsTimestamp::decode(be_u64(data, 32), bigtime);
let mtime = XfsTimestamp::decode(be_u64(data, 40), bigtime);
let ctime = XfsTimestamp::decode(be_u64(data, 48), bigtime);
let (crtime, cowextsize, di_ino, uuid, crc) = if is_v3 {
let mut u = [0u8; 16];
if let Some(s) = data.get(160..176) {
u.copy_from_slice(s);
}
(
Some(XfsTimestamp::decode(be_u64(data, 144), bigtime)),
Some(be_u32(data, 128)),
Some(be_u64(data, 152)),
Some(u),
Some(u32::from_le_bytes([
u8_at(data, 100),
u8_at(data, 101),
u8_at(data, 102),
u8_at(data, 103),
])),
)
} else {
(None, None, None, None, None)
};
let fork_off = if is_v3 {
V3_LITINO_OFFSET
} else {
V2_LITINO_OFFSET
};
let data_fork = data.get(fork_off..).unwrap_or(&[]).to_vec();
let crc_valid = crc_status(is_v3, data, DINODE_CRC_OFF);
Ok(Self {
magic,
mode: be_u16(data, 2),
version,
format: InodeFormat::from_raw(u8_at(data, 5)),
aformat: u8_at(data, 83),
size: be_u64(data, 56),
nblocks: be_u64(data, 64),
nextents: be_u32(data, 76),
aextents: be_u16(data, 80),
forkoff: u8_at(data, 82),
atime,
mtime,
ctime,
crtime,
flags2,
cowextsize,
di_ino,
uuid,
crc,
crc_valid,
data_fork,
})
}
#[must_use]
pub fn file_type(&self) -> FileType {
FileType::from_mode(self.mode)
}
#[must_use]
pub fn is_dir(&self) -> bool {
self.file_type() == FileType::Directory
}
#[must_use]
pub fn is_reg(&self) -> bool {
self.file_type() == FileType::Regular
}
#[must_use]
pub fn is_bigtime(&self) -> bool {
self.flags2.is_some_and(|f| f & XFS_DIFLAG2_BIGTIME != 0)
}
#[must_use]
pub fn data_fork_offset(&self) -> usize {
if self.version >= 3 {
V3_LITINO_OFFSET
} else {
V2_LITINO_OFFSET
}
}
}