Debian Codegraph @

debian/missing-sources/rustix/src/process/types.rs

raw
1 //! Types for use with [`rustix::process`] functions.
2 //!
3 //! [`rustix::process`]: crate::process
4
5 #![allow(unsafe_code)]
6
7 use crate::backend::c;
8 use crate::pid::Pid;
9 use 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)]
16 pub 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"))]
31 impl 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"))]
54 impl 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 /// `F_*LCK` constants for use with [`fcntl_getlk`].
67 ///
68 /// [`fcntl_getlk`]: crate::process::fcntl_getlk()
69 #[cfg(not(target_os = "horizon"))]
70 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
71 #[repr(i16)]
72 pub enum FlockType {
73 /// `F_RDLCK`
74 ReadLock = c::F_RDLCK as _,
75 /// `F_WRLCK`
76 WriteLock = c::F_WRLCK as _,
77 /// `F_UNLCK`
78 Unlocked = c::F_UNLCK as _,
79 }
80
81 /// `F_SEEK*` constants for use with [`fcntl_getlk`].
82 ///
83 /// [`fcntl_getlk`]: crate::process::fcntl_getlk()
84 #[cfg(not(target_os = "horizon"))]
85 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
86 #[repr(i16)]
87 pub enum FlockOffsetType {
88 /// `F_SEEK_SET`
89 Set = c::SEEK_SET as _,
90 /// `F_SEEK_CUR`
91 Current = c::SEEK_CUR as _,
92 /// `F_SEEK_END`
93 End = c::SEEK_END as _,
94 }