1 // Copyright 2014 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)]
19 pub use self::imp::Lock;
23 use std::ffi::CString;
27 #[cfg(target_os = "linux")]
32 pub l_type: libc::c_short,
33 pub l_whence: libc::c_short,
34 pub l_start: libc::off_t,
35 pub l_len: libc::off_t,
36 pub l_pid: libc::pid_t,
38 // not actually here, but brings in line with freebsd
39 pub l_sysid: libc::c_int,
42 pub const F_WRLCK: libc::c_short = 1;
43 pub const F_UNLCK: libc::c_short = 2;
44 pub const F_SETLK: libc::c_int = 6;
45 pub const F_SETLKW: libc::c_int = 7;
48 #[cfg(target_os = "freebsd")]
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,
58 pub l_sysid: libc::c_int,
61 pub const F_UNLCK: libc::c_short = 2;
62 pub const F_WRLCK: libc::c_short = 3;
63 pub const F_SETLK: libc::c_int = 12;
64 pub const F_SETLKW: libc::c_int = 13;
67 #[cfg(target_os = "dragonfly")]
72 pub l_start: libc::off_t,
73 pub l_len: libc::off_t,
74 pub l_pid: libc::pid_t,
75 pub l_type: libc::c_short,
76 pub l_whence: libc::c_short,
78 // not actually here, but brings in line with freebsd
79 pub l_sysid: libc::c_int,
82 pub const F_UNLCK: libc::c_short = 2;
83 pub const F_WRLCK: libc::c_short = 3;
84 pub const F_SETLK: libc::c_int = 8;
85 pub const F_SETLKW: libc::c_int = 9;
88 #[cfg(any(target_os = "macos", target_os = "ios"))]
93 pub l_start: libc::off_t,
94 pub l_len: libc::off_t,
95 pub l_pid: libc::pid_t,
96 pub l_type: libc::c_short,
97 pub l_whence: libc::c_short,
99 // not actually here, but brings in line with freebsd
100 pub l_sysid: libc::c_int,
103 pub const F_UNLCK: libc::c_short = 2;
104 pub const F_WRLCK: libc::c_short = 3;
105 pub const F_SETLK: libc::c_int = 8;
106 pub const F_SETLKW: libc::c_int = 9;
114 pub fn new(p: &Path) -> Lock {
115 let buf = CString::from_slice(p.as_vec());
117 libc::open(buf.as_ptr(), libc::O_RDWR | libc::O_CREAT,
120 assert!(fd > 0, "failed to open lockfile: [{}] {}",
121 stdos::errno(), stdos::last_os_error());
122 let flock = os::flock {
126 l_whence: libc::SEEK_SET as libc::c_short,
131 libc::fcntl(fd, os::F_SETLKW, &flock)
134 let errno = stdos::errno();
135 unsafe { libc::close(fd); }
136 panic!("could not lock `{}`: [{}] {}", p.display(),
137 errno, stdos::error_string(errno))
145 let flock = os::flock {
149 l_whence: libc::SEEK_SET as libc::c_short,
154 libc::fcntl(self.fd, os::F_SETLK, &flock);
155 libc::close(self.fd);
168 static LOCKFILE_EXCLUSIVE_LOCK: libc::DWORD = 0x00000002;
170 #[allow(non_snake_case)]
172 fn LockFileEx(hFile: libc::HANDLE,
173 dwFlags: libc::DWORD,
174 dwReserved: libc::DWORD,
175 nNumberOfBytesToLockLow: libc::DWORD,
176 nNumberOfBytesToLockHigh: libc::DWORD,
177 lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
178 fn UnlockFileEx(hFile: libc::HANDLE,
179 dwReserved: libc::DWORD,
180 nNumberOfBytesToLockLow: libc::DWORD,
181 nNumberOfBytesToLockHigh: libc::DWORD,
182 lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
186 handle: libc::HANDLE,
190 pub fn new(p: &Path) -> Lock {
191 let mut p_16: Vec<u16> = p.as_str().unwrap().utf16_units().collect();
193 let handle = unsafe {
194 libc::CreateFileW(p_16.as_ptr(),
195 libc::FILE_GENERIC_READ |
196 libc::FILE_GENERIC_WRITE,
197 libc::FILE_SHARE_READ |
198 libc::FILE_SHARE_DELETE |
199 libc::FILE_SHARE_WRITE,
202 libc::FILE_ATTRIBUTE_NORMAL,
205 if handle == libc::INVALID_HANDLE_VALUE {
206 panic!("create file error: [{}] {}",
207 os::errno(), os::last_os_error());
209 let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
211 LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0,
215 let errno = os::errno();
216 unsafe { libc::CloseHandle(handle); }
217 panic!("could not lock `{}`: [{}] {}", p.display(),
218 errno, os::error_string(errno));
220 Lock { handle: handle }
226 let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
228 UnlockFileEx(self.handle, 0, 100, 0, &mut overlapped);
229 libc::CloseHandle(self.handle);