| 1 | use std::fs::File; |
| 2 | use std::io; |
| 3 | |
| 4 | /// Upper bound on the bytes read back from a lock file's holder record. |
| 5 | pub const OWNER_LOCK_HOLDER_MAX_BYTES: u64 = 128; |
| 6 | |
| 7 | /// The Windows byte range `LockFile` takes: `(offset low, offset high, length |
| 8 | /// low, length high)`. A Windows byte-range lock is mandatory, so locking the |
| 9 | /// whole file would stop a contender reading the holder record at the front |
| 10 | /// (#6573). The range starts after the record and still overlaps the |
| 11 | /// whole-file range older builds take, so mixed versions exclude each other. |
| 12 | #[cfg(windows)] |
| 13 | const OWNER_LOCK_RANGE: (u32, u32, u32, u32) = |
| 14 | (OWNER_LOCK_HOLDER_MAX_BYTES as u32, 0, u32::MAX, 0x7FFF_FFFF); |
| 15 | |
| 16 | pub fn is_contention(error: &std::io::Error) -> bool { |
| 17 | error.kind() == std::io::ErrorKind::WouldBlock || matches!(error.raw_os_error(), Some(32 | 33)) |
| 18 | } |
| 19 | |
| 20 | /// Whether a failed advisory lock means the filesystem has none at all, as |
| 21 | /// opposed to a lock that is genuinely held by someone else. |
| 22 | /// |
| 23 | /// `EWOULDBLOCK` is deliberately absent: a held lock must still wait or fail |
| 24 | /// rather than fall back. The rest are the mount saying "no locking here" — |
| 25 | /// HarmonyOS hmdfs answers `EPERM` for `flock` and `fcntl(F_SETLK)` on every |
| 26 | /// file, and FUSE and some network mounts answer the same way. |
| 27 | /// |
| 28 | /// A caller that degrades instead of failing must still refuse when |
| 29 | /// [`is_contention`] is true; the two predicates are disjoint by construction. |
| 30 | #[cfg(unix)] |
| 31 | pub fn advisory_locking_unavailable(error: &std::io::Error) -> bool { |
| 32 | matches!( |
| 33 | error.raw_os_error(), |
| 34 | Some(libc::EPERM) |
| 35 | | Some(libc::EACCES) |
| 36 | | Some(libc::ENOTSUP) |
| 37 | | Some(libc::EINVAL) |
| 38 | | Some(libc::ENOSYS) |
| 39 | ) |
| 40 | } |
| 41 | |
| 42 | pub fn try_lock_exclusive(file: &File) -> io::Result<()> { |
| 43 | #[cfg(unix)] |
| 44 | { |
| 45 | use std::os::fd::AsRawFd as _; |
| 46 | // SAFETY: `file` owns a valid descriptor and is retained by the |
| 47 | // caller while the lock is held. |
| 48 | if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } != 0 { |
| 49 | return Err(io::Error::last_os_error()); |
| 50 | } |
| 51 | Ok(()) |
| 52 | } |
| 53 | #[cfg(windows)] |
| 54 | { |
| 55 | use std::os::windows::io::AsRawHandle as _; |
| 56 | use windows_sys::Win32::Storage::FileSystem::LockFile; |
| 57 | let (offset_low, offset_high, length_low, length_high) = OWNER_LOCK_RANGE; |
| 58 | // SAFETY: `file` owns a valid handle retained by the caller. |
| 59 | if unsafe { |
| 60 | LockFile( |
| 61 | file.as_raw_handle() as _, |
| 62 | offset_low, |
| 63 | offset_high, |
| 64 | length_low, |
| 65 | length_high, |
| 66 | ) |
| 67 | } == 0 |
| 68 | { |
| 69 | return Err(io::Error::last_os_error()); |
| 70 | } |
| 71 | Ok(()) |
| 72 | } |
| 73 | #[cfg(not(any(unix, windows)))] |
| 74 | { |
| 75 | let _ = file; |
| 76 | Ok(()) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | pub fn unlock(file: &File) { |
| 81 | #[cfg(unix)] |
| 82 | { |
| 83 | use std::os::fd::AsRawFd as _; |
| 84 | // SAFETY: `file` owns a valid descriptor for the duration of the call. |
| 85 | unsafe { |
| 86 | libc::flock(file.as_raw_fd(), libc::LOCK_UN); |
| 87 | } |
| 88 | } |
| 89 | #[cfg(windows)] |
| 90 | { |
| 91 | use std::os::windows::io::AsRawHandle as _; |
| 92 | use windows_sys::Win32::Storage::FileSystem::UnlockFile; |
| 93 | let (offset_low, offset_high, length_low, length_high) = OWNER_LOCK_RANGE; |
| 94 | // SAFETY: `file` owns a valid handle for the duration of the call. |
| 95 | unsafe { |
| 96 | UnlockFile( |
| 97 | file.as_raw_handle() as _, |
| 98 | offset_low, |
| 99 | offset_high, |
| 100 | length_low, |
| 101 | length_high, |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | #[cfg(not(any(unix, windows)))] |
| 106 | { |
| 107 | let _ = file; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// Try to take an exclusive OS lock on `file` without blocking: `Ok(true)` |
| 112 | /// when acquired (released when the file closes), `Ok(false)` when another |
| 113 | /// open file description holds it. |
| 114 | pub fn try_lock_file_exclusive(file: &File) -> std::io::Result<bool> { |
| 115 | match try_lock_exclusive(file) { |
| 116 | Ok(()) => Ok(true), |
| 117 | Err(error) if is_contention(&error) => Ok(false), |
| 118 | Err(error) => Err(error), |
| 119 | } |
| 120 | } |
| 121 |