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::*;
18 #[cfg(any(target_os = "linux", target_os = "android"))]
22 pub l_type: libc::c_short,
23 pub l_whence: libc::c_short,
24 pub l_start: libc::off_t,
25 pub l_len: libc::off_t,
26 pub l_pid: libc::pid_t,
28 // not actually here, but brings in line with freebsd
29 pub l_sysid: libc::c_int,
33 #[cfg(target_os = "freebsd")]
37 pub l_start: libc::off_t,
38 pub l_len: libc::off_t,
39 pub l_pid: libc::pid_t,
40 pub l_type: libc::c_short,
41 pub l_whence: libc::c_short,
42 pub l_sysid: libc::c_int,
46 #[cfg(any(target_os = "dragonfly",
49 target_os = "openbsd"))]
53 pub l_start: libc::off_t,
54 pub l_len: libc::off_t,
55 pub l_pid: libc::pid_t,
56 pub l_type: libc::c_short,
57 pub l_whence: libc::c_short,
59 // not actually here, but brings in line with freebsd
60 pub l_sysid: libc::c_int,
64 #[cfg(target_os = "haiku")]
68 pub l_type: libc::c_short,
69 pub l_whence: libc::c_short,
70 pub l_start: libc::off_t,
71 pub l_len: libc::off_t,
72 pub l_pid: libc::pid_t,
74 // not actually here, but brings in line with freebsd
75 pub l_sysid: libc::c_int,
79 #[cfg(any(target_os = "macos", target_os = "ios"))]
83 pub l_start: libc::off_t,
84 pub l_len: libc::off_t,
85 pub l_pid: libc::pid_t,
86 pub l_type: libc::c_short,
87 pub l_whence: libc::c_short,
89 // not actually here, but brings in line with freebsd
90 pub l_sysid: libc::c_int,
94 #[cfg(target_os = "solaris")]
98 pub l_type: libc::c_short,
99 pub l_whence: libc::c_short,
100 pub l_start: libc::off_t,
101 pub l_len: libc::off_t,
102 pub l_sysid: libc::c_int,
103 pub l_pid: libc::pid_t,
117 -> io::Result<Lock> {
118 let os: &OsStr = p.as_ref();
119 let buf = CString::new(os.as_bytes()).unwrap();
120 let open_flags = if create {
121 libc::O_RDWR | libc::O_CREAT
127 libc::open(buf.as_ptr(), open_flags,
128 libc::S_IRWXU as libc::c_int)
132 return Err(io::Error::last_os_error());
135 let lock_type = if exclusive {
136 libc::F_WRLCK as libc::c_short
138 libc::F_RDLCK as libc::c_short
141 let flock = os::flock {
145 l_whence: libc::SEEK_SET as libc::c_short,
149 let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
151 libc::fcntl(fd, cmd, &flock)
154 let err = io::Error::last_os_error();
155 unsafe { libc::close(fd); }
165 let flock = os::flock {
169 l_whence: libc::SEEK_SET as libc::c_short,
170 l_type: libc::F_UNLCK as libc::c_short,
174 libc::fcntl(self.fd, libc::F_SETLK, &flock);
175 libc::close(self.fd);
179 } else if #[cfg(windows)] {
181 use std::os::windows::prelude::*;
182 use std::os::windows::raw::HANDLE;
183 use std::fs::{File, OpenOptions};
184 use std::os::raw::{c_ulong, c_int};
186 type DWORD = c_ulong;
188 type ULONG_PTR = usize;
190 type LPOVERLAPPED = *mut OVERLAPPED;
191 const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x0000_0002;
192 const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x0000_0001;
194 const FILE_SHARE_DELETE: DWORD = 0x4;
195 const FILE_SHARE_READ: DWORD = 0x1;
196 const FILE_SHARE_WRITE: DWORD = 0x2;
201 InternalHigh: ULONG_PTR,
208 fn LockFileEx(hFile: HANDLE,
211 nNumberOfBytesToLockLow: DWORD,
212 nNumberOfBytesToLockHigh: DWORD,
213 lpOverlapped: LPOVERLAPPED) -> BOOL;
226 -> io::Result<Lock> {
227 assert!(p.parent().unwrap().exists(),
228 "Parent directory of lock-file must exist: {}",
231 let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
233 let mut open_options = OpenOptions::new();
234 open_options.read(true)
235 .share_mode(share_mode);
238 open_options.create(true)
242 debug!("Attempting to open lock file `{}`", p.display());
243 let file = match open_options.open(p) {
245 debug!("Lock file opened successfully");
249 debug!("Error opening lock file: {}", err);
255 let mut overlapped: OVERLAPPED = mem::zeroed();
259 dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
263 dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
266 debug!("Attempting to acquire lock on lock file `{}`",
268 LockFileEx(file.as_raw_handle(),
276 let err = io::Error::last_os_error();
277 debug!("Failed acquiring file lock: {}", err);
280 debug!("Successfully acquired lock.");
281 Ok(Lock { _file: file })
286 // Note that we don't need a Drop impl on the Windows: The file is unlocked
287 // automatically when it's closed.
293 pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool)
296 let msg = "file locks not supported on this platform";
297 Err(io::Error::new(io::ErrorKind::Other, msg))
304 pub fn panicking_new(p: &Path,
309 Lock::new(p, wait, create, exclusive).unwrap_or_else(|err| {
310 panic!("could not lock `{}`: {}", p.display(), err);