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;
25 #[cfg(target_os = "linux")]
30 l_type: libc::c_short,
31 l_whence: libc::c_short,
36 // not actually here, but brings in line with freebsd
40 pub static F_WRLCK: libc::c_short = 1;
41 pub static F_UNLCK: libc::c_short = 2;
42 pub static F_SETLK: libc::c_int = 6;
43 pub static F_SETLKW: libc::c_int = 7;
46 #[cfg(target_os = "freebsd")]
54 l_type: libc::c_short,
55 l_whence: libc::c_short,
59 pub static F_UNLCK: libc::c_short = 2;
60 pub static F_WRLCK: libc::c_short = 3;
61 pub static F_SETLK: libc::c_int = 12;
62 pub static F_SETLKW: libc::c_int = 13;
65 #[cfg(target_os = "macos")]
73 l_type: libc::c_short,
74 l_whence: libc::c_short,
76 // not actually here, but brings in line with freebsd
80 pub static F_UNLCK: libc::c_short = 2;
81 pub static F_WRLCK: libc::c_short = 3;
82 pub static F_SETLK: libc::c_int = 8;
83 pub static F_SETLKW: libc::c_int = 9;
91 pub fn new(p: &Path) -> Lock {
92 let fd = p.with_c_str(|s| unsafe {
93 libc::open(s, libc::O_RDWR | libc::O_CREAT, libc::S_IRWXU)
96 let flock = os::flock {
100 l_whence: libc::SEEK_SET as libc::c_short,
105 libc::fcntl(fd, os::F_SETLKW, &flock as *os::flock)
108 unsafe { libc::close(fd); }
109 fail!("could not lock `{}`", p.display())
117 let flock = os::flock {
121 l_whence: libc::SEEK_SET as libc::c_short,
126 libc::fcntl(self.fd, os::F_SETLK, &flock as *os::flock);
127 libc::close(self.fd);
137 use std::os::win32::as_utf16_p;
141 static LOCKFILE_EXCLUSIVE_LOCK: libc::DWORD = 0x00000002;
144 fn LockFileEx(hFile: libc::HANDLE,
145 dwFlags: libc::DWORD,
146 dwReserved: libc::DWORD,
147 nNumberOfBytesToLockLow: libc::DWORD,
148 nNumberOfBytesToLockHigh: libc::DWORD,
149 lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
150 fn UnlockFileEx(hFile: libc::HANDLE,
151 dwReserved: libc::DWORD,
152 nNumberOfBytesToLockLow: libc::DWORD,
153 nNumberOfBytesToLockHigh: libc::DWORD,
154 lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
158 priv handle: libc::HANDLE,
162 pub fn new(p: &Path) -> Lock {
163 let handle = as_utf16_p(p.as_str().unwrap(), |p| unsafe {
165 libc::FILE_GENERIC_READ |
166 libc::FILE_GENERIC_WRITE,
167 libc::FILE_SHARE_READ |
168 libc::FILE_SHARE_DELETE |
169 libc::FILE_SHARE_WRITE,
172 libc::FILE_ATTRIBUTE_NORMAL,
175 if handle as uint == libc::INVALID_HANDLE_VALUE as uint {
176 fail!("create file error: {}", os::last_os_error());
178 let mut overlapped: libc::OVERLAPPED = unsafe { mem::init() };
180 LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0,
184 unsafe { libc::CloseHandle(handle); }
185 fail!("could not lock `{}`: {}", p.display(),
188 Lock { handle: handle }
194 let mut overlapped: libc::OVERLAPPED = unsafe { mem::init() };
196 UnlockFileEx(self.handle, 0, 100, 0, &mut overlapped);
197 libc::CloseHandle(self.handle);