1#![allow(unsafe_code)]
6
7#[cfg(not(target_os = "horizon"))]
8use crate::backend::c;
9#[cfg(not(target_os = "horizon"))]
10use crate::pid::Pid;
11#[cfg(not(target_os = "horizon"))]
12use core::mem::transmute;
13
14#[cfg(not(target_os = "horizon"))]
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub struct Flock {
20 pub start: u64,
22 pub length: u64,
24 pub pid: Option<Pid>,
27 pub typ: FlockType,
29 pub offset_type: FlockOffsetType,
31}
32
33#[cfg(not(target_os = "horizon"))]
34impl Flock {
35 pub(crate) const unsafe fn from_raw_unchecked(raw_fl: c::flock) -> Self {
36 #[cfg(not(all(target_os = "hurd", target_arch = "x86")))]
37 {
38 Self {
39 start: raw_fl.l_start as _,
40 length: raw_fl.l_len as _,
41 pid: Pid::from_raw(raw_fl.l_pid),
42 typ: transmute::<i16, FlockType>(raw_fl.l_type),
43 offset_type: transmute::<i16, FlockOffsetType>(raw_fl.l_whence),
44 }
45 }
46 #[cfg(all(target_os = "hurd", target_arch = "x86"))]
47 {
48 Self {
49 start: raw_fl.l_start as _,
50 length: raw_fl.l_len as _,
51 pid: Pid::from_raw(raw_fl.l_pid),
52 typ: transmute::<i32, FlockType>(raw_fl.l_type),
53 offset_type: transmute::<i32, FlockOffsetType>(raw_fl.l_whence),
54 }
55 }
56 }
57
58 pub(crate) fn as_raw(&self) -> c::flock {
59 let mut f: c::flock = unsafe { core::mem::zeroed() };
60 f.l_start = self.start as _;
61 f.l_len = self.length as _;
62 f.l_pid = Pid::as_raw(self.pid);
63 f.l_type = self.typ as _;
64 f.l_whence = self.offset_type as _;
65 f
66 }
67}
68
69#[cfg(not(target_os = "horizon"))]
70impl From<FlockType> for Flock {
71 fn from(value: FlockType) -> Self {
72 Self {
73 start: 0,
74 length: 0,
75 pid: None,
76 typ: value,
77 offset_type: FlockOffsetType::Set,
78 }
79 }
80}
81
82#[cfg(not(target_os = "horizon"))]
86#[derive(Clone, Copy, Debug, PartialEq, Eq)]
87#[cfg_attr(not(all(target_os = "hurd", target_arch = "x86")), repr(i16))]
88#[cfg_attr(all(target_os = "hurd", target_arch = "x86"), repr(i32))]
89pub enum FlockType {
90 ReadLock = c::F_RDLCK as _,
92 WriteLock = c::F_WRLCK as _,
94 Unlocked = c::F_UNLCK as _,
96}
97
98#[cfg(not(target_os = "horizon"))]
102#[derive(Clone, Copy, Debug, PartialEq, Eq)]
103#[cfg_attr(not(all(target_os = "hurd", target_arch = "x86")), repr(i16))]
104#[cfg_attr(all(target_os = "hurd", target_arch = "x86"), repr(i32))]
105pub enum FlockOffsetType {
106 Set = c::SEEK_SET as _,
108 Current = c::SEEK_CUR as _,
110 End = c::SEEK_END as _,
112}