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;
24 use std::c_str::ToCStr;
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 const F_WRLCK: libc::c_short = 1;
42 pub const F_UNLCK: libc::c_short = 2;
43 pub const F_SETLK: libc::c_int = 6;
44 pub const 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 const F_UNLCK: libc::c_short = 2;
61 pub const F_WRLCK: libc::c_short = 3;
62 pub const F_SETLK: libc::c_int = 12;
63 pub const F_SETLKW: libc::c_int = 13;
66 #[cfg(target_os = "dragonfly")]
71 pub l_start: libc::off_t,
72 pub l_len: libc::off_t,
73 pub l_pid: libc::pid_t,
74 pub l_type: libc::c_short,
75 pub l_whence: libc::c_short,
77 // not actually here, but brings in line with freebsd
78 pub l_sysid: libc::c_int,
81 pub const F_UNLCK: libc::c_short = 2;
82 pub const F_WRLCK: libc::c_short = 3;
83 pub const F_SETLK: libc::c_int = 8;
84 pub const F_SETLKW: libc::c_int = 9;
87 #[cfg(any(target_os = "macos", target_os = "ios"))]
92 pub l_start: libc::off_t,
93 pub l_len: libc::off_t,
94 pub l_pid: libc::pid_t,
95 pub l_type: libc::c_short,
96 pub l_whence: libc::c_short,
98 // not actually here, but brings in line with freebsd
99 pub l_sysid: libc::c_int,
102 pub const F_UNLCK: libc::c_short = 2;
103 pub const F_WRLCK: libc::c_short = 3;
104 pub const F_SETLK: libc::c_int = 8;
105 pub const F_SETLKW: libc::c_int = 9;
113 pub fn new(p: &Path) -> Lock {
114 let fd = p.with_c_str(|s| unsafe {
115 libc::open(s, libc::O_RDWR | libc::O_CREAT, libc::S_IRWXU)
118 let flock = os::flock {
122 l_whence: libc::SEEK_SET as libc::c_short,
127 libc::fcntl(fd, os::F_SETLKW, &flock as *const os::flock)
130 unsafe { libc::close(fd); }
131 panic!("could not lock `{}`", p.display())
139 let flock = os::flock {
143 l_whence: libc::SEEK_SET as libc::c_short,
148 libc::fcntl(self.fd, os::F_SETLK, &flock as *const os::flock);
149 libc::close(self.fd);
162 static LOCKFILE_EXCLUSIVE_LOCK: libc::DWORD = 0x00000002;
164 #[allow(non_snake_case)]
166 fn LockFileEx(hFile: libc::HANDLE,
167 dwFlags: libc::DWORD,
168 dwReserved: libc::DWORD,
169 nNumberOfBytesToLockLow: libc::DWORD,
170 nNumberOfBytesToLockHigh: libc::DWORD,
171 lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
172 fn UnlockFileEx(hFile: libc::HANDLE,
173 dwReserved: libc::DWORD,
174 nNumberOfBytesToLockLow: libc::DWORD,
175 nNumberOfBytesToLockHigh: libc::DWORD,
176 lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
180 handle: libc::HANDLE,
184 pub fn new(p: &Path) -> Lock {
185 let mut p_16: Vec<u16> = p.as_str().unwrap().utf16_units().collect();
187 let handle = unsafe {
188 libc::CreateFileW(p_16.as_ptr(),
189 libc::FILE_GENERIC_READ |
190 libc::FILE_GENERIC_WRITE,
191 libc::FILE_SHARE_READ |
192 libc::FILE_SHARE_DELETE |
193 libc::FILE_SHARE_WRITE,
196 libc::FILE_ATTRIBUTE_NORMAL,
199 if handle == libc::INVALID_HANDLE_VALUE {
200 panic!("create file error: {}", os::last_os_error());
202 let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
204 LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0,
208 unsafe { libc::CloseHandle(handle); }
209 panic!("could not lock `{}`: {}", p.display(),
212 Lock { handle: handle }
218 let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
220 UnlockFileEx(self.handle, 0, 100, 0, &mut overlapped);
221 libc::CloseHandle(self.handle);