1#![allow(unsafe_code)]
6
7use crate::backend::c;
8use crate::pid::Pid;
9use core::mem::transmute;
10
11#[cfg(not(target_os = "horizon"))]
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub struct Flock {
17 pub start: u64,
19 pub length: u64,
21 pub pid: Option<Pid>,
24 pub typ: FlockType,
26 pub offset_type: FlockOffsetType,
28}
29
30#[cfg(not(target_os = "horizon"))]
31impl Flock {
32 pub(crate) const unsafe fn from_raw_unchecked(raw_fl: c::flock) -> Self {
33 Self {
34 start: raw_fl.l_start as _,
35 length: raw_fl.l_len as _,
36 pid: Pid::from_raw(raw_fl.l_pid),
37 typ: transmute::<i16, FlockType>(raw_fl.l_type),
38 offset_type: transmute::<i16, FlockOffsetType>(raw_fl.l_whence),
39 }
40 }
41
42 pub(crate) fn as_raw(&self) -> c::flock {
43 let mut f: c::flock = unsafe { core::mem::zeroed() };
44 f.l_start = self.start as _;
45 f.l_len = self.length as _;
46 f.l_pid = Pid::as_raw(self.pid);
47 f.l_type = self.typ as _;
48 f.l_whence = self.offset_type as _;
49 f
50 }
51}
52
53#[cfg(not(target_os = "horizon"))]
54impl From<FlockType> for Flock {
55 fn from(value: FlockType) -> Self {
56 Self {
57 start: 0,
58 length: 0,
59 pid: None,
60 typ: value,
61 offset_type: FlockOffsetType::Set,
62 }
63 }
64}
65
66#[cfg(not(target_os = "horizon"))]
70#[derive(Clone, Copy, Debug, PartialEq, Eq)]
71#[repr(i16)]
72pub enum FlockType {
73 ReadLock = c::F_RDLCK as _,
75 WriteLock = c::F_WRLCK as _,
77 Unlocked = c::F_UNLCK as _,
79}
80
81#[cfg(not(target_os = "horizon"))]
85#[derive(Clone, Copy, Debug, PartialEq, Eq)]
86#[repr(i16)]
87pub enum FlockOffsetType {
88 Set = c::SEEK_SET as _,
90 Current = c::SEEK_CUR as _,
92 End = c::SEEK_END as _,
94}