1 //! Simple file-locking apis for each OS.
3 //! This is not meant to be in the standard library, it does nothing with
4 //! green/native threading. This is just a bare-bones enough solution for
5 //! librustdoc, it is not production quality at all.
7 #![allow(non_camel_case_types)]
8 #![allow(nonstandard_style)]
10 use std::fs::{File, OpenOptions};
15 // We use `flock` rather than `fcntl` on Linux, because WSL1 does not support
16 // `fcntl`-style advisory locks properly (rust-lang/rust#72157).
18 // For other Unix targets we still use `fcntl` because it's more portable than
20 if #[cfg(target_os = "linux")] {
21 use std::os::unix::prelude::*;
34 let file = OpenOptions::new()
38 .mode(libc::S_IRWXU as u32)
41 let mut operation = if exclusive {
47 operation |= libc::LOCK_NB
50 let ret = unsafe { libc::flock(file.as_raw_fd(), operation) };
52 Err(io::Error::last_os_error())
54 Ok(Lock { _file: file })
58 pub fn error_unsupported(err: &io::Error) -> bool {
59 matches!(err.raw_os_error(), Some(libc::ENOTSUP) | Some(libc::ENOSYS))
63 // Note that we don't need a Drop impl to execute `flock(fd, LOCK_UN)`. Lock acquired by
64 // `flock` is associated with the file descriptor and closing the file release it
66 } else if #[cfg(unix)] {
68 use std::os::unix::prelude::*;
81 let file = OpenOptions::new()
85 .mode(libc::S_IRWXU as u32)
88 let lock_type = if exclusive {
94 let mut flock: libc::flock = unsafe { mem::zeroed() };
95 flock.l_type = lock_type as libc::c_short;
96 flock.l_whence = libc::SEEK_SET as libc::c_short;
100 let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
102 libc::fcntl(file.as_raw_fd(), cmd, &flock)
105 Err(io::Error::last_os_error())
111 pub fn error_unsupported(err: &io::Error) -> bool {
112 matches!(err.raw_os_error(), Some(libc::ENOTSUP) | Some(libc::ENOSYS))
118 let mut flock: libc::flock = unsafe { mem::zeroed() };
119 flock.l_type = libc::F_UNLCK as libc::c_short;
120 flock.l_whence = libc::SEEK_SET as libc::c_short;
125 libc::fcntl(self.file.as_raw_fd(), libc::F_SETLK, &flock);
129 } else if #[cfg(windows)] {
131 use std::os::windows::prelude::*;
133 use winapi::shared::winerror::ERROR_INVALID_FUNCTION;
134 use winapi::um::minwinbase::{OVERLAPPED, LOCKFILE_FAIL_IMMEDIATELY, LOCKFILE_EXCLUSIVE_LOCK};
135 use winapi::um::fileapi::LockFileEx;
136 use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE};
148 -> io::Result<Lock> {
149 assert!(p.parent().unwrap().exists(),
150 "Parent directory of lock-file must exist: {}",
153 let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
155 let mut open_options = OpenOptions::new();
156 open_options.read(true)
157 .share_mode(share_mode);
160 open_options.create(true)
164 debug!("attempting to open lock file `{}`", p.display());
165 let file = match open_options.open(p) {
167 debug!("lock file opened successfully");
171 debug!("error opening lock file: {}", err);
177 let mut overlapped: OVERLAPPED = mem::zeroed();
181 dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
185 dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
188 debug!("attempting to acquire lock on lock file `{}`",
190 LockFileEx(file.as_raw_handle(),
198 let err = io::Error::last_os_error();
199 debug!("failed acquiring file lock: {}", err);
202 debug!("successfully acquired lock");
203 Ok(Lock { _file: file })
207 pub fn error_unsupported(err: &io::Error) -> bool {
208 err.raw_os_error() == Some(ERROR_INVALID_FUNCTION as i32)
212 // Note that we don't need a Drop impl on the Windows: The file is unlocked
213 // automatically when it's closed.
219 pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool)
222 let msg = "file locks not supported on this platform";
223 Err(io::Error::new(io::ErrorKind::Other, msg))
226 pub fn error_unsupported(_err: &io::Error) -> bool {