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)]
15 use std::ffi::{CString, OsStr};
16 use std::os::unix::prelude::*;
19 #[cfg(any(target_os = "linux", target_os = "android"))]
25 pub l_type: libc::c_short,
26 pub l_whence: libc::c_short,
27 pub l_start: libc::off_t,
28 pub l_len: libc::off_t,
29 pub l_pid: libc::pid_t,
31 // not actually here, but brings in line with freebsd
32 pub l_sysid: libc::c_int,
36 #[cfg(target_os = "freebsd")]
42 pub l_start: libc::off_t,
43 pub l_len: libc::off_t,
44 pub l_pid: libc::pid_t,
45 pub l_type: libc::c_short,
46 pub l_whence: libc::c_short,
47 pub l_sysid: libc::c_int,
51 #[cfg(any(target_os = "dragonfly",
54 target_os = "openbsd"))]
60 pub l_start: libc::off_t,
61 pub l_len: libc::off_t,
62 pub l_pid: libc::pid_t,
63 pub l_type: libc::c_short,
64 pub l_whence: libc::c_short,
66 // not actually here, but brings in line with freebsd
67 pub l_sysid: libc::c_int,
71 #[cfg(target_os = "haiku")]
77 pub l_type: libc::c_short,
78 pub l_whence: libc::c_short,
79 pub l_start: libc::off_t,
80 pub l_len: libc::off_t,
81 pub l_pid: libc::pid_t,
83 // not actually here, but brings in line with freebsd
84 pub l_sysid: libc::c_int,
88 #[cfg(any(target_os = "macos", target_os = "ios"))]
94 pub l_start: libc::off_t,
95 pub l_len: libc::off_t,
96 pub l_pid: libc::pid_t,
97 pub l_type: libc::c_short,
98 pub l_whence: libc::c_short,
100 // not actually here, but brings in line with freebsd
101 pub l_sysid: libc::c_int,
105 #[cfg(target_os = "solaris")]
111 pub l_type: libc::c_short,
112 pub l_whence: libc::c_short,
113 pub l_start: libc::off_t,
114 pub l_len: libc::off_t,
115 pub l_sysid: libc::c_int,
116 pub l_pid: libc::pid_t,
130 -> io::Result<Lock> {
131 let os: &OsStr = p.as_ref();
132 let buf = CString::new(os.as_bytes()).unwrap();
133 let open_flags = if create {
134 libc::O_RDWR | libc::O_CREAT
140 libc::open(buf.as_ptr(), open_flags,
141 libc::S_IRWXU as libc::c_int)
145 return Err(io::Error::last_os_error());
148 let lock_type = if exclusive {
149 libc::F_WRLCK as libc::c_short
151 libc::F_RDLCK as libc::c_short
154 let flock = os::flock {
158 l_whence: libc::SEEK_SET as libc::c_short,
162 let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
164 libc::fcntl(fd, cmd, &flock)
167 let err = io::Error::last_os_error();
168 unsafe { libc::close(fd); }
178 let flock = os::flock {
182 l_whence: libc::SEEK_SET as libc::c_short,
183 l_type: libc::F_UNLCK as libc::c_short,
187 libc::fcntl(self.fd, libc::F_SETLK, &flock);
188 libc::close(self.fd);
192 } else if #[cfg(windows)] {
194 use std::os::windows::prelude::*;
195 use std::os::windows::raw::HANDLE;
196 use std::fs::{File, OpenOptions};
197 use std::os::raw::{c_ulong, c_int};
199 type DWORD = c_ulong;
201 type ULONG_PTR = usize;
203 type LPOVERLAPPED = *mut OVERLAPPED;
204 const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x0000_0002;
205 const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x0000_0001;
207 const FILE_SHARE_DELETE: DWORD = 0x4;
208 const FILE_SHARE_READ: DWORD = 0x1;
209 const FILE_SHARE_WRITE: DWORD = 0x2;
214 InternalHigh: ULONG_PTR,
221 fn LockFileEx(hFile: HANDLE,
224 nNumberOfBytesToLockLow: DWORD,
225 nNumberOfBytesToLockHigh: DWORD,
226 lpOverlapped: LPOVERLAPPED) -> BOOL;
239 -> io::Result<Lock> {
240 assert!(p.parent().unwrap().exists(),
241 "Parent directory of lock-file must exist: {}",
244 let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
246 let mut open_options = OpenOptions::new();
247 open_options.read(true)
248 .share_mode(share_mode);
251 open_options.create(true)
255 debug!("Attempting to open lock file `{}`", p.display());
256 let file = match open_options.open(p) {
258 debug!("Lock file opened successfully");
262 debug!("Error opening lock file: {}", err);
268 let mut overlapped: OVERLAPPED = mem::zeroed();
272 dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
276 dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
279 debug!("Attempting to acquire lock on lock file `{}`",
281 LockFileEx(file.as_raw_handle(),
289 let err = io::Error::last_os_error();
290 debug!("Failed acquiring file lock: {}", err);
293 debug!("Successfully acquired lock.");
294 Ok(Lock { _file: file })
299 // Note that we don't need a Drop impl on the Windows: The file is unlocked
300 // automatically when it's closed.
306 pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool)
309 let msg = "file locks not supported on this platform";
310 Err(io::Error::new(io::ErrorKind::Other, msg))
317 pub fn panicking_new(p: &Path,
322 Lock::new(p, wait, create, exclusive).unwrap_or_else(|err| {
323 panic!("could not lock `{}`: {}", p.display(), err);