#![deny(clippy::undocumented_unsafe_blocks)]
use std::fs::File;
use std::io;
use std::mem::size_of;
use std::os::fd::AsRawFd;
use rustix::io::Errno;
const BUFFER_WORDS: usize = 512;
const BUFFER_LEN: usize = size_of::<[u64; BUFFER_WORDS]>();
const REFERENCE_LEN: usize = size_of::<libc::attrreference_t>();
#[derive(Debug)]
pub(crate) enum Fetched {
Complete(Box<[u8]>),
Truncated,
}
fn extended_security_list() -> libc::attrlist {
libc::attrlist {
bitmapcount: libc::ATTR_BIT_MAP_COUNT,
reserved: 0,
commonattr: libc::ATTR_CMN_EXTENDED_SECURITY,
volattr: 0,
dirattr: 0,
fileattr: 0,
forkattr: 0,
}
}
pub(crate) fn fetch(file: &File) -> io::Result<Fetched> {
let mut list = extended_security_list();
let mut buffer = [0_u64; BUFFER_WORDS];
let result = unsafe {
libc::fgetattrlist(
file.as_raw_fd(),
(&raw mut list).cast(),
buffer.as_mut_ptr().cast(),
BUFFER_LEN,
libc::FSOPT_REPORT_FULLSIZE,
)
};
if result != 0 {
let error = io::Error::last_os_error();
return match error.raw_os_error() {
Some(code) if code == Errno::RANGE.raw_os_error() => Ok(Fetched::Truncated),
_ => Err(error),
};
}
let bytes: Vec<u8> = buffer.iter().flat_map(|word| word.to_ne_bytes()).collect();
let length = bytes
.first_chunk::<4>()
.map(|length| u32::from_ne_bytes(*length))
.and_then(|length| usize::try_from(length).ok());
let complete = length.and_then(|length| bytes.get(..length)).map(Box::from);
Ok(complete.map_or(Fetched::Truncated, Fetched::Complete))
}
pub(crate) fn store(file: &File, filesec: &[u8]) -> io::Result<()> {
let too_large = || io::Error::from(io::ErrorKind::InvalidInput);
let total = REFERENCE_LEN
.checked_add(filesec.len())
.filter(|total| *total <= BUFFER_LEN)
.ok_or_else(too_large)?;
let reference = libc::attrreference_t {
attr_dataoffset: i32::try_from(REFERENCE_LEN).map_err(|_| too_large())?,
attr_length: u32::try_from(filesec.len()).map_err(|_| too_large())?,
};
let mut bytes = Vec::with_capacity(total);
bytes.extend_from_slice(&reference.attr_dataoffset.to_ne_bytes());
bytes.extend_from_slice(&reference.attr_length.to_ne_bytes());
bytes.extend_from_slice(filesec);
let mut words: Vec<u64> = bytes
.chunks(size_of::<u64>())
.map(|chunk| {
let mut word = [0_u8; size_of::<u64>()];
for (target, byte) in word.iter_mut().zip(chunk) {
*target = *byte;
}
u64::from_ne_bytes(word)
})
.collect();
let mut list = extended_security_list();
let result = unsafe {
libc::fsetattrlist(
file.as_raw_fd(),
(&raw mut list).cast(),
words.as_mut_ptr().cast(),
total,
0,
)
};
if result == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}