rustix/process/
types.rs

1//! Types for use with [`rustix::process`] functions.
2//!
3//! [`rustix::process`]: crate::process
4
5#![allow(unsafe_code)]
6
7use crate::backend::c;
8use crate::pid::Pid;
9use core::mem::transmute;
10
11/// File lock data structure used in [`fcntl_getlk`].
12///
13/// [`fcntl_getlk`]: crate::process::fcntl_getlk()
14#[cfg(not(target_os = "horizon"))]
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub struct Flock {
17    /// Starting offset for lock
18    pub start: u64,
19    /// Number of bytes to lock
20    pub length: u64,
21    /// PID of process blocking our lock. If set to `None`, it refers to the
22    /// current process
23    pub pid: Option<Pid>,
24    /// Type of lock
25    pub typ: FlockType,
26    /// Offset type of lock
27    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/// `F_*LCK` constants for use with [`fcntl_getlk`].
80///
81/// [`fcntl_getlk`]: crate::process::fcntl_getlk()
82#[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    /// `F_RDLCK`
88    ReadLock = c::F_RDLCK as _,
89    /// `F_WRLCK`
90    WriteLock = c::F_WRLCK as _,
91    /// `F_UNLCK`
92    Unlocked = c::F_UNLCK as _,
93}
94
95/// `F_SEEK*` constants for use with [`fcntl_getlk`].
96///
97/// [`fcntl_getlk`]: crate::process::fcntl_getlk()
98#[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    /// `F_SEEK_SET`
104    Set = c::SEEK_SET as _,
105    /// `F_SEEK_CUR`
106    Current = c::SEEK_CUR as _,
107    /// `F_SEEK_END`
108    End = c::SEEK_END as _,
109}