#![cfg(unix)]
use std::os::unix::io::RawFd;
use super::error::IpcError;
#[cfg(any(target_os = "linux", target_os = "android"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Reservation {
Reserved,
Unsupported,
Interrupted,
Failed(i32),
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub(super) const fn classify_reservation(code: i32) -> Reservation {
match code {
0 => Reservation::Reserved,
libc::EINTR => Reservation::Interrupted,
libc::EOPNOTSUPP | libc::ENOSYS | libc::EINVAL => Reservation::Unsupported,
other => Reservation::Failed(other),
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub(super) fn reserve_backing_store(fd: RawFd, length: libc::off_t) -> Result<(), IpcError> {
loop {
let code = unsafe { libc::posix_fallocate(fd, 0, length) };
match classify_reservation(code) {
Reservation::Reserved | Reservation::Unsupported => return Ok(()),
Reservation::Interrupted => {}
Reservation::Failed(code) => return Err(IpcError::SystemError(code)),
}
}
}
#[cfg(all(unix, not(any(target_os = "linux", target_os = "android"))))]
#[expect(
clippy::unnecessary_wraps,
reason = "matches the fallible Linux signature so `create` stays platform-uniform"
)]
pub(super) fn reserve_backing_store(_fd: RawFd, _length: libc::off_t) -> Result<(), IpcError> {
Ok(())
}