]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/flock.rs
rollup merge of #21438: taralx/kill-racycell
[rust.git] / src / librustdoc / flock.rs
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.
4 //
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.
10
11 //! Simple file-locking apis for each OS.
12 //!
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.
16
17 #![allow(non_camel_case_types)]
18
19 pub use self::imp::Lock;
20
21 #[cfg(unix)]
22 mod imp {
23     use std::ffi::CString;
24     use libc;
25     use std::os as stdos;
26
27     #[cfg(target_os = "linux")]
28     mod os {
29         use libc;
30
31         pub struct flock {
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,
37
38             // not actually here, but brings in line with freebsd
39             pub l_sysid: libc::c_int,
40         }
41
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;
46     }
47
48     #[cfg(target_os = "freebsd")]
49     mod os {
50         use libc;
51
52         pub struct flock {
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,
59         }
60
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;
65     }
66
67     #[cfg(target_os = "dragonfly")]
68     mod os {
69         use libc;
70
71         pub struct flock {
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,
77
78             // not actually here, but brings in line with freebsd
79             pub l_sysid: libc::c_int,
80         }
81
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;
86     }
87
88     #[cfg(any(target_os = "macos", target_os = "ios"))]
89     mod os {
90         use libc;
91
92         pub struct flock {
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,
98
99             // not actually here, but brings in line with freebsd
100             pub l_sysid: libc::c_int,
101         }
102
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;
107     }
108
109     pub struct Lock {
110         fd: libc::c_int,
111     }
112
113     impl Lock {
114         pub fn new(p: &Path) -> Lock {
115             let buf = CString::from_slice(p.as_vec());
116             let fd = unsafe {
117                 libc::open(buf.as_ptr(), libc::O_RDWR | libc::O_CREAT,
118                            libc::S_IRWXU)
119             };
120             assert!(fd > 0, "failed to open lockfile: [{}] {}",
121                     stdos::errno(), stdos::last_os_error());
122             let flock = os::flock {
123                 l_start: 0,
124                 l_len: 0,
125                 l_pid: 0,
126                 l_whence: libc::SEEK_SET as libc::c_short,
127                 l_type: os::F_WRLCK,
128                 l_sysid: 0,
129             };
130             let ret = unsafe {
131                 libc::fcntl(fd, os::F_SETLKW, &flock)
132             };
133             if ret == -1 {
134                 let errno = stdos::errno();
135                 unsafe { libc::close(fd); }
136                 panic!("could not lock `{}`: [{}] {}", p.display(),
137                        errno, stdos::error_string(errno))
138             }
139             Lock { fd: fd }
140         }
141     }
142
143     impl Drop for Lock {
144         fn drop(&mut self) {
145             let flock = os::flock {
146                 l_start: 0,
147                 l_len: 0,
148                 l_pid: 0,
149                 l_whence: libc::SEEK_SET as libc::c_short,
150                 l_type: os::F_UNLCK,
151                 l_sysid: 0,
152             };
153             unsafe {
154                 libc::fcntl(self.fd, os::F_SETLK, &flock);
155                 libc::close(self.fd);
156             }
157         }
158     }
159 }
160
161 #[cfg(windows)]
162 mod imp {
163     use libc;
164     use std::mem;
165     use std::os;
166     use std::ptr;
167
168     static LOCKFILE_EXCLUSIVE_LOCK: libc::DWORD = 0x00000002;
169
170     #[allow(non_snake_case)]
171     extern "system" {
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;
183     }
184
185     pub struct Lock {
186         handle: libc::HANDLE,
187     }
188
189     impl Lock {
190         pub fn new(p: &Path) -> Lock {
191             let mut p_16: Vec<u16> = p.as_str().unwrap().utf16_units().collect();
192             p_16.push(0);
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,
200                                   ptr::null_mut(),
201                                   libc::CREATE_ALWAYS,
202                                   libc::FILE_ATTRIBUTE_NORMAL,
203                                   ptr::null_mut())
204             };
205             if handle == libc::INVALID_HANDLE_VALUE {
206                 panic!("create file error: [{}] {}",
207                        os::errno(), os::last_os_error());
208             }
209             let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
210             let ret = unsafe {
211                 LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0,
212                            &mut overlapped)
213             };
214             if ret == 0 {
215                 let errno = os::errno();
216                 unsafe { libc::CloseHandle(handle); }
217                 panic!("could not lock `{}`: [{}] {}", p.display(),
218                        errno, os::error_string(errno));
219             }
220             Lock { handle: handle }
221         }
222     }
223
224     impl Drop for Lock {
225         fn drop(&mut self) {
226             let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
227             unsafe {
228                 UnlockFileEx(self.handle, 0, 100, 0, &mut overlapped);
229                 libc::CloseHandle(self.handle);
230             }
231         }
232     }
233 }