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;
26 #[cfg(target_os = "linux")]
31 pub l_type: libc::c_short,
32 pub l_whence: libc::c_short,
33 pub l_start: libc::off_t,
34 pub l_len: libc::off_t,
35 pub l_pid: libc::pid_t,
37 // not actually here, but brings in line with freebsd
38 pub l_sysid: libc::c_int,
41 pub static F_WRLCK: libc::c_short = 1;
42 pub static F_UNLCK: libc::c_short = 2;
43 pub static F_SETLK: libc::c_int = 6;
44 pub static F_SETLKW: libc::c_int = 7;
47 #[cfg(target_os = "freebsd")]
52 pub l_start: libc::off_t,
53 pub l_len: libc::off_t,
54 pub l_pid: libc::pid_t,
55 pub l_type: libc::c_short,
56 pub l_whence: libc::c_short,
57 pub l_sysid: libc::c_int,
60 pub static F_UNLCK: libc::c_short = 2;
61 pub static F_WRLCK: libc::c_short = 3;
62 pub static F_SETLK: libc::c_int = 12;
63 pub static F_SETLKW: libc::c_int = 13;
66 #[cfg(target_os = "macos")]
67 #[cfg(target_os = "ios")]
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 static F_UNLCK: libc::c_short = 2;
83 pub static F_WRLCK: libc::c_short = 3;
84 pub static F_SETLK: libc::c_int = 8;
85 pub static F_SETLKW: libc::c_int = 9;
93 pub fn new(p: &Path) -> Lock {
94 let fd = p.with_c_str(|s| unsafe {
95 libc::open(s, libc::O_RDWR | libc::O_CREAT, libc::S_IRWXU)
98 let flock = os::flock {
102 l_whence: libc::SEEK_SET as libc::c_short,
107 libc::fcntl(fd, os::F_SETLKW, &flock as *const os::flock)
110 unsafe { libc::close(fd); }
111 fail!("could not lock `{}`", p.display())
119 let flock = os::flock {
123 l_whence: libc::SEEK_SET as libc::c_short,
128 libc::fcntl(self.fd, os::F_SETLK, &flock as *const os::flock);
129 libc::close(self.fd);
142 static LOCKFILE_EXCLUSIVE_LOCK: libc::DWORD = 0x00000002;
144 #[allow(non_snake_case_functions)]
146 fn LockFileEx(hFile: libc::HANDLE,
147 dwFlags: libc::DWORD,
148 dwReserved: libc::DWORD,
149 nNumberOfBytesToLockLow: libc::DWORD,
150 nNumberOfBytesToLockHigh: libc::DWORD,
151 lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
152 fn UnlockFileEx(hFile: libc::HANDLE,
153 dwReserved: libc::DWORD,
154 nNumberOfBytesToLockLow: libc::DWORD,
155 nNumberOfBytesToLockHigh: libc::DWORD,
156 lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
160 handle: libc::HANDLE,
164 pub fn new(p: &Path) -> Lock {
165 let p_16 = p.as_str().unwrap().to_utf16().append_one(0);
166 let handle = unsafe {
167 libc::CreateFileW(p_16.as_ptr(),
168 libc::FILE_GENERIC_READ |
169 libc::FILE_GENERIC_WRITE,
170 libc::FILE_SHARE_READ |
171 libc::FILE_SHARE_DELETE |
172 libc::FILE_SHARE_WRITE,
175 libc::FILE_ATTRIBUTE_NORMAL,
178 if handle as uint == libc::INVALID_HANDLE_VALUE as uint {
179 fail!("create file error: {}", os::last_os_error());
181 let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
183 LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0,
187 unsafe { libc::CloseHandle(handle); }
188 fail!("could not lock `{}`: {}", p.display(),
191 Lock { handle: handle }
197 let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
199 UnlockFileEx(self.handle, 0, 100, 0, &mut overlapped);
200 libc::CloseHandle(self.handle);