#![allow(unsafe_code)]
use crate::backend::c;
use crate::pid::Pid;
use core::mem::transmute;
#[cfg(not(target_os = "horizon"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Flock {
pub start: u64,
pub length: u64,
pub pid: Option<Pid>,
pub typ: FlockType,
pub offset_type: FlockOffsetType,
}
#[cfg(not(target_os = "horizon"))]
impl Flock {
pub(crate) const unsafe fn from_raw_unchecked(raw_fl: c::flock) -> Self {
Self {
start: raw_fl.l_start as _,
length: raw_fl.l_len as _,
pid: Pid::from_raw(raw_fl.l_pid),
typ: transmute::<i16, FlockType>(raw_fl.l_type),
offset_type: transmute::<i16, FlockOffsetType>(raw_fl.l_whence),
}
}
pub(crate) fn as_raw(&self) -> c::flock {
let mut f: c::flock = unsafe { core::mem::zeroed() };
f.l_start = self.start as _;
f.l_len = self.length as _;
f.l_pid = Pid::as_raw(self.pid);
f.l_type = self.typ as _;
f.l_whence = self.offset_type as _;
f
}
}
#[cfg(not(target_os = "horizon"))]
impl From<FlockType> for Flock {
fn from(value: FlockType) -> Self {
Self {
start: 0,
length: 0,
pid: None,
typ: value,
offset_type: FlockOffsetType::Set,
}
}
}
#[cfg(not(target_os = "horizon"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(i16)]
pub enum FlockType {
ReadLock = c::F_RDLCK as _,
WriteLock = c::F_WRLCK as _,
Unlocked = c::F_UNLCK as _,
}
#[cfg(not(target_os = "horizon"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(i16)]
pub enum FlockOffsetType {
Set = c::SEEK_SET as _,
Current = c::SEEK_CUR as _,
End = c::SEEK_END as _,
}