1 //! We use `flock` rather than `fcntl` on Linux, because WSL1 does not support
2 //! `fcntl`-style advisory locks properly (rust-lang/rust#72157). For other Unix
3 //! targets we still use `fcntl` because it's more portable than `flock`.
5 use std::fs::{File, OpenOptions};
7 use std::os::unix::prelude::*;
16 pub fn new(p: &Path, wait: bool, create: bool, exclusive: bool) -> io::Result<Lock> {
17 let file = OpenOptions::new().read(true).write(true).create(create).mode(0o600).open(p)?;
19 let mut operation = if exclusive { libc::LOCK_EX } else { libc::LOCK_SH };
21 operation |= libc::LOCK_NB
24 let ret = unsafe { libc::flock(file.as_raw_fd(), operation) };
25 if ret == -1 { Err(io::Error::last_os_error()) } else { Ok(Lock { _file: file }) }
28 pub fn error_unsupported(err: &io::Error) -> bool {
29 matches!(err.raw_os_error(), Some(libc::ENOTSUP) | Some(libc::ENOSYS))
33 // Note that we don't need a Drop impl to execute `flock(fd, LOCK_UN)`. A lock acquired by
34 // `flock` is associated with the file descriptor and closing the file releases it