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 #[cfg(not(all(target_os = "hurd", target_arch = "x86")))]
34 {
35 Self {
36 start: raw_fl.l_start as _,
37 length: raw_fl.l_len as _,
38 pid: Pid::from_raw(raw_fl.l_pid),
39 typ: transmute::<i16, FlockType>(raw_fl.l_type),
40 offset_type: transmute::<i16, FlockOffsetType>(raw_fl.l_whence),
41 }
42 }
43 #[cfg(all(target_os = "hurd", target_arch = "x86"))]
44 {
45 Self {
46 start: raw_fl.l_start as _,
47 length: raw_fl.l_len as _,
48 pid: Pid::from_raw(raw_fl.l_pid),
49 typ: transmute::<i32, FlockType>(raw_fl.l_type),
50 offset_type: transmute::<i32, FlockOffsetType>(raw_fl.l_whence),
51 }
52 }
53 }
54
55 pub(crate) fn as_raw(&self) -> c::flock {
56 let mut f: c::flock = unsafe { core::mem::zeroed() };
57 f.l_start = self.start as _;
58 f.l_len = self.length as _;
59 f.l_pid = Pid::as_raw(self.pid);
60 f.l_type = self.typ as _;
61 f.l_whence = self.offset_type as _;
62 f
63 }
64}
65
66#[cfg(not(target_os = "horizon"))]
67impl From<FlockType> for Flock {
68 fn from(value: FlockType) -> Self {
69 Self {
70 start: 0,
71 length: 0,
72 pid: None,
73 typ: value,
74 offset_type: FlockOffsetType::Set,
75 }
76 }
77}
78
79#[cfg(not(target_os = "horizon"))]
83#[derive(Clone, Copy, Debug, PartialEq, Eq)]
84#[cfg_attr(not(all(target_os = "hurd", target_arch = "x86")), repr(i16))]
85#[cfg_attr(all(target_os = "hurd", target_arch = "x86"), repr(i32))]
86pub enum FlockType {
87 ReadLock = c::F_RDLCK as _,
89 WriteLock = c::F_WRLCK as _,
91 Unlocked = c::F_UNLCK as _,
93}
94
95#[cfg(not(target_os = "horizon"))]
99#[derive(Clone, Copy, Debug, PartialEq, Eq)]
100#[cfg_attr(not(all(target_os = "hurd", target_arch = "x86")), repr(i16))]
101#[cfg_attr(all(target_os = "hurd", target_arch = "x86"), repr(i32))]
102pub enum FlockOffsetType {
103 Set = c::SEEK_SET as _,
105 Current = c::SEEK_CUR as _,
107 End = c::SEEK_END as _,
109}