]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/flock.rs
rollup merge of #20581: apasel422/extend
[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
26     #[cfg(target_os = "linux")]
27     mod os {
28         use libc;
29
30         pub struct flock {
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,
36
37             // not actually here, but brings in line with freebsd
38             pub l_sysid: libc::c_int,
39         }
40
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;
45     }
46
47     #[cfg(target_os = "freebsd")]
48     mod os {
49         use libc;
50
51         pub struct flock {
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,
58         }
59
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;
64     }
65
66     #[cfg(target_os = "dragonfly")]
67     mod os {
68         use libc;
69
70         pub struct flock {
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,
76
77             // not actually here, but brings in line with freebsd
78             pub l_sysid: libc::c_int,
79         }
80
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;
85     }
86
87     #[cfg(any(target_os = "macos", target_os = "ios"))]
88     mod os {
89         use libc;
90
91         pub struct flock {
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,
97
98             // not actually here, but brings in line with freebsd
99             pub l_sysid: libc::c_int,
100         }
101
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;
106     }
107
108     pub struct Lock {
109         fd: libc::c_int,
110     }
111
112     impl Lock {
113         pub fn new(p: &Path) -> Lock {
114             let buf = CString::from_slice(p.as_vec());
115             let fd = unsafe {
116                 libc::open(buf.as_ptr(), libc::O_RDWR | libc::O_CREAT,
117                            libc::S_IRWXU)
118             };
119             assert!(fd > 0);
120             let flock = os::flock {
121                 l_start: 0,
122                 l_len: 0,
123                 l_pid: 0,
124                 l_whence: libc::SEEK_SET as libc::c_short,
125                 l_type: os::F_WRLCK,
126                 l_sysid: 0,
127             };
128             let ret = unsafe {
129                 libc::fcntl(fd, os::F_SETLKW, &flock as *const os::flock)
130             };
131             if ret == -1 {
132                 unsafe { libc::close(fd); }
133                 panic!("could not lock `{}`", p.display())
134             }
135             Lock { fd: fd }
136         }
137     }
138
139     impl Drop for Lock {
140         fn drop(&mut self) {
141             let flock = os::flock {
142                 l_start: 0,
143                 l_len: 0,
144                 l_pid: 0,
145                 l_whence: libc::SEEK_SET as libc::c_short,
146                 l_type: os::F_UNLCK,
147                 l_sysid: 0,
148             };
149             unsafe {
150                 libc::fcntl(self.fd, os::F_SETLK, &flock as *const os::flock);
151                 libc::close(self.fd);
152             }
153         }
154     }
155 }
156
157 #[cfg(windows)]
158 mod imp {
159     use libc;
160     use std::mem;
161     use std::os;
162     use std::ptr;
163
164     static LOCKFILE_EXCLUSIVE_LOCK: libc::DWORD = 0x00000002;
165
166     #[allow(non_snake_case)]
167     extern "system" {
168         fn LockFileEx(hFile: libc::HANDLE,
169                       dwFlags: libc::DWORD,
170                       dwReserved: libc::DWORD,
171                       nNumberOfBytesToLockLow: libc::DWORD,
172                       nNumberOfBytesToLockHigh: libc::DWORD,
173                       lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
174         fn UnlockFileEx(hFile: libc::HANDLE,
175                         dwReserved: libc::DWORD,
176                         nNumberOfBytesToLockLow: libc::DWORD,
177                         nNumberOfBytesToLockHigh: libc::DWORD,
178                         lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
179     }
180
181     pub struct Lock {
182         handle: libc::HANDLE,
183     }
184
185     impl Lock {
186         pub fn new(p: &Path) -> Lock {
187             let mut p_16: Vec<u16> = p.as_str().unwrap().utf16_units().collect();
188             p_16.push(0);
189             let handle = unsafe {
190                 libc::CreateFileW(p_16.as_ptr(),
191                                   libc::FILE_GENERIC_READ |
192                                     libc::FILE_GENERIC_WRITE,
193                                   libc::FILE_SHARE_READ |
194                                     libc::FILE_SHARE_DELETE |
195                                     libc::FILE_SHARE_WRITE,
196                                   ptr::null_mut(),
197                                   libc::CREATE_ALWAYS,
198                                   libc::FILE_ATTRIBUTE_NORMAL,
199                                   ptr::null_mut())
200             };
201             if handle == libc::INVALID_HANDLE_VALUE {
202                 panic!("create file error: {}", os::last_os_error());
203             }
204             let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
205             let ret = unsafe {
206                 LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0,
207                            &mut overlapped)
208             };
209             if ret == 0 {
210                 unsafe { libc::CloseHandle(handle); }
211                 panic!("could not lock `{}`: {}", p.display(),
212                       os::last_os_error())
213             }
214             Lock { handle: handle }
215         }
216     }
217
218     impl Drop for Lock {
219         fn drop(&mut self) {
220             let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
221             unsafe {
222                 UnlockFileEx(self.handle, 0, 100, 0, &mut overlapped);
223                 libc::CloseHandle(self.handle);
224             }
225         }
226     }
227 }