|
| 1 | +#![allow(unsafe_code)] |
| 2 | + |
| 3 | +use crate::backend::c; |
| 4 | +use crate::pid::Pid; |
| 5 | +use core::mem::transmute; |
| 6 | + |
| 7 | +/// File lock data structure used in [`fcntl_getlk`]. |
| 8 | +/// |
| 9 | +/// [`fcntl_getlk`]: crate::fs::fcntl_getlk |
| 10 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 11 | +pub struct Flock { |
| 12 | + /// Starting offset for lock |
| 13 | + pub start: u64, |
| 14 | + /// Number of bytes to lock |
| 15 | + pub length: u64, |
| 16 | + /// PID of process blocking our lock. If set to `None`, it refers to the current process |
| 17 | + pub pid: Option<Pid>, |
| 18 | + /// Type of lock |
| 19 | + pub typ: FlockType, |
| 20 | + /// Offset type of lock |
| 21 | + pub offset_type: FlockOffsetType, |
| 22 | +} |
| 23 | + |
| 24 | +impl Flock { |
| 25 | + pub(crate) const unsafe fn from_raw_unchecked(raw_fl: c::flock) -> Flock { |
| 26 | + Flock { |
| 27 | + start: raw_fl.l_start as _, |
| 28 | + length: raw_fl.l_len as _, |
| 29 | + pid: transmute(raw_fl.l_pid), |
| 30 | + typ: transmute(raw_fl.l_type), |
| 31 | + offset_type: transmute(raw_fl.l_whence), |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + pub(crate) fn as_raw(&self) -> c::flock { |
| 36 | + let mut f: c::flock = unsafe { core::mem::zeroed() }; |
| 37 | + f.l_start = self.start as _; |
| 38 | + f.l_len = self.length as _; |
| 39 | + f.l_pid = unsafe { transmute(self.pid) }; |
| 40 | + f.l_type = self.typ as _; |
| 41 | + f.l_whence = self.offset_type as _; |
| 42 | + f |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl From<FlockType> for Flock { |
| 47 | + fn from(value: FlockType) -> Self { |
| 48 | + Flock { |
| 49 | + start: 0, |
| 50 | + length: 0, |
| 51 | + pid: None, |
| 52 | + typ: value, |
| 53 | + offset_type: FlockOffsetType::Set, |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// `F_*LCK` constants for use with [`fcntl_getlk`]. |
| 59 | +/// |
| 60 | +/// [`fcntl_getlk`]: crate::fs::fcntl_getlk |
| 61 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 62 | +#[repr(i16)] |
| 63 | +pub enum FlockType { |
| 64 | + /// `F_RDLCK` |
| 65 | + ReadLock = c::F_RDLCK as _, |
| 66 | + /// `F_WRLCK` |
| 67 | + WriteLock = c::F_WRLCK as _, |
| 68 | + /// `F_UNLCK` |
| 69 | + Unlocked = c::F_UNLCK as _, |
| 70 | +} |
| 71 | + |
| 72 | +/// `F_SEEK*` constants for use with [`fcntl_getlk`]. |
| 73 | +/// |
| 74 | +/// [`fcntl_getlk`]: crate::fs::fcntl_getlk |
| 75 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 76 | +#[repr(i16)] |
| 77 | +pub enum FlockOffsetType { |
| 78 | + /// `F_SEEK_SET` |
| 79 | + Set = c::SEEK_SET as _, |
| 80 | + /// `F_SEEK_CUR` |
| 81 | + Current = c::SEEK_CUR as _, |
| 82 | + /// `F_SEEK_END` |
| 83 | + End = c::SEEK_END as _, |
| 84 | +} |
0 commit comments