Tries to acquire a lock using [`fcntl`] with respect to the given parameters. Please note that `fcntl()` OFD locks are **advisory locks**, which do not prevent to `open()` a file if a lock is already placed. # Parameters - `file`: The file to acquire a lock for [`LockType`]. The file's state will be logically mutated, but not technically. - `lock_type`: The [`LockType`] - `granularity`: The [`Lo
(
file: &Fd,
lock_type: LockType,
granularity: LockGranularity,
)
| 195 | /// - `lock_type`: The [`LockType`] |
| 196 | /// - `granularity`: The [`LockGranularity`]. |
| 197 | pub fn try_acquire_lock<Fd: AsRawFd>( |
| 198 | file: &Fd, |
| 199 | lock_type: LockType, |
| 200 | granularity: LockGranularity, |
| 201 | ) -> Result<(), LockError> { |
| 202 | let flock = get_flock(lock_type, granularity); |
| 203 | |
| 204 | let res = fcntl(file.as_raw_fd(), FcntlArg::F_OFD_SETLK(&flock)); |
| 205 | match res { |
| 206 | 0 => Ok(()), |
| 207 | -1 => { |
| 208 | let io_error = io::Error::last_os_error(); |
| 209 | let errno = io_error.raw_os_error().unwrap(); |
| 210 | match errno { |
| 211 | // See man page for error code: |
| 212 | // <https://man7.org/linux/man-pages/man2/fcntl.2.html> |
| 213 | libc::EAGAIN | libc::EACCES => Err(LockError::AlreadyLocked), |
| 214 | _ => Err(LockError::Io(io_error)), |
| 215 | } |
| 216 | } |
| 217 | val => panic!("Unexpected return value from fcntl(): {val}"), |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// Clears a lock. |
| 222 | /// |
no test coverage detected