1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Simple file-locking apis for each OS.
13 //! This is not meant to be in the standard library, it does nothing with
14 //! green/native threading. This is just a bare-bones enough solution for
15 //! librustdoc, it is not production quality at all.
17 #![allow(non_camel_case_types)]
18 #![allow(nonstandard_style)]
25 use std::ffi::{CString, OsStr};
26 use std::os::unix::prelude::*;
29 #[cfg(any(target_os = "linux", target_os = "android"))]
35 pub l_type: libc::c_short,
36 pub l_whence: libc::c_short,
37 pub l_start: libc::off_t,
38 pub l_len: libc::off_t,
39 pub l_pid: libc::pid_t,
41 // not actually here, but brings in line with freebsd
42 pub l_sysid: libc::c_int,
45 pub const F_RDLCK: libc::c_short = 0;
46 pub const F_WRLCK: libc::c_short = 1;
47 pub const F_UNLCK: libc::c_short = 2;
48 pub const F_SETLK: libc::c_int = 6;
49 pub const F_SETLKW: libc::c_int = 7;
52 #[cfg(target_os = "freebsd")]
58 pub l_start: libc::off_t,
59 pub l_len: libc::off_t,
60 pub l_pid: libc::pid_t,
61 pub l_type: libc::c_short,
62 pub l_whence: libc::c_short,
63 pub l_sysid: libc::c_int,
66 pub const F_RDLCK: libc::c_short = 1;
67 pub const F_UNLCK: libc::c_short = 2;
68 pub const F_WRLCK: libc::c_short = 3;
69 pub const F_SETLK: libc::c_int = 12;
70 pub const F_SETLKW: libc::c_int = 13;
73 #[cfg(any(target_os = "dragonfly",
76 target_os = "openbsd"))]
82 pub l_start: libc::off_t,
83 pub l_len: libc::off_t,
84 pub l_pid: libc::pid_t,
85 pub l_type: libc::c_short,
86 pub l_whence: libc::c_short,
88 // not actually here, but brings in line with freebsd
89 pub l_sysid: libc::c_int,
92 pub const F_RDLCK: libc::c_short = 1;
93 pub const F_UNLCK: libc::c_short = 2;
94 pub const F_WRLCK: libc::c_short = 3;
95 pub const F_SETLK: libc::c_int = 8;
96 pub const F_SETLKW: libc::c_int = 9;
99 #[cfg(target_os = "haiku")]
105 pub l_type: libc::c_short,
106 pub l_whence: libc::c_short,
107 pub l_start: libc::off_t,
108 pub l_len: libc::off_t,
109 pub l_pid: libc::pid_t,
111 // not actually here, but brings in line with freebsd
112 pub l_sysid: libc::c_int,
115 pub const F_RDLCK: libc::c_short = 0x0040;
116 pub const F_UNLCK: libc::c_short = 0x0200;
117 pub const F_WRLCK: libc::c_short = 0x0400;
118 pub const F_SETLK: libc::c_int = 0x0080;
119 pub const F_SETLKW: libc::c_int = 0x0100;
122 #[cfg(any(target_os = "macos", target_os = "ios"))]
128 pub l_start: libc::off_t,
129 pub l_len: libc::off_t,
130 pub l_pid: libc::pid_t,
131 pub l_type: libc::c_short,
132 pub l_whence: libc::c_short,
134 // not actually here, but brings in line with freebsd
135 pub l_sysid: libc::c_int,
138 pub const F_RDLCK: libc::c_short = 1;
139 pub const F_UNLCK: libc::c_short = 2;
140 pub const F_WRLCK: libc::c_short = 3;
141 pub const F_SETLK: libc::c_int = 8;
142 pub const F_SETLKW: libc::c_int = 9;
145 #[cfg(target_os = "solaris")]
151 pub l_type: libc::c_short,
152 pub l_whence: libc::c_short,
153 pub l_start: libc::off_t,
154 pub l_len: libc::off_t,
155 pub l_sysid: libc::c_int,
156 pub l_pid: libc::pid_t,
159 pub const F_RDLCK: libc::c_short = 1;
160 pub const F_WRLCK: libc::c_short = 2;
161 pub const F_UNLCK: libc::c_short = 3;
162 pub const F_SETLK: libc::c_int = 6;
163 pub const F_SETLKW: libc::c_int = 7;
176 -> io::Result<Lock> {
177 let os: &OsStr = p.as_ref();
178 let buf = CString::new(os.as_bytes()).unwrap();
179 let open_flags = if create {
180 libc::O_RDWR | libc::O_CREAT
186 libc::open(buf.as_ptr(), open_flags,
187 libc::S_IRWXU as libc::c_int)
191 return Err(io::Error::last_os_error());
194 let lock_type = if exclusive {
200 let flock = os::flock {
204 l_whence: libc::SEEK_SET as libc::c_short,
208 let cmd = if wait { os::F_SETLKW } else { os::F_SETLK };
210 libc::fcntl(fd, cmd, &flock)
213 let err = io::Error::last_os_error();
214 unsafe { libc::close(fd); }
224 let flock = os::flock {
228 l_whence: libc::SEEK_SET as libc::c_short,
233 libc::fcntl(self.fd, os::F_SETLK, &flock);
234 libc::close(self.fd);
238 } else if #[cfg(windows)] {
240 use std::os::windows::prelude::*;
241 use std::os::windows::raw::HANDLE;
242 use std::fs::{File, OpenOptions};
243 use std::os::raw::{c_ulong, c_int};
245 type DWORD = c_ulong;
247 type ULONG_PTR = usize;
249 type LPOVERLAPPED = *mut OVERLAPPED;
250 const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x0000_0002;
251 const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x0000_0001;
253 const FILE_SHARE_DELETE: DWORD = 0x4;
254 const FILE_SHARE_READ: DWORD = 0x1;
255 const FILE_SHARE_WRITE: DWORD = 0x2;
260 InternalHigh: ULONG_PTR,
267 fn LockFileEx(hFile: HANDLE,
270 nNumberOfBytesToLockLow: DWORD,
271 nNumberOfBytesToLockHigh: DWORD,
272 lpOverlapped: LPOVERLAPPED) -> BOOL;
285 -> io::Result<Lock> {
286 assert!(p.parent().unwrap().exists(),
287 "Parent directory of lock-file must exist: {}",
290 let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
292 let mut open_options = OpenOptions::new();
293 open_options.read(true)
294 .share_mode(share_mode);
297 open_options.create(true)
301 debug!("Attempting to open lock file `{}`", p.display());
302 let file = match open_options.open(p) {
304 debug!("Lock file opened successfully");
308 debug!("Error opening lock file: {}", err);
314 let mut overlapped: OVERLAPPED = mem::zeroed();
318 dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
322 dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
325 debug!("Attempting to acquire lock on lock file `{}`",
327 LockFileEx(file.as_raw_handle(),
335 let err = io::Error::last_os_error();
336 debug!("Failed acquiring file lock: {}", err);
339 debug!("Successfully acquired lock.");
340 Ok(Lock { _file: file })
345 // Note that we don't need a Drop impl on the Windows: The file is unlocked
346 // automatically when it's closed.
352 pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool)
355 let msg = "file locks not supported on this platform";
356 Err(io::Error::new(io::ErrorKind::Other, msg))
363 pub fn panicking_new(p: &Path,
368 Lock::new(p, wait, create, exclusive).unwrap_or_else(|err| {
369 panic!("could not lock `{}`: {}", p.display(), err);