]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/flock.rs
Add hooks for Miri panic unwinding
[rust.git] / src / librustc_data_structures / flock.rs
1 //! Simple file-locking apis for each OS.
2 //!
3 //! This is not meant to be in the standard library, it does nothing with
4 //! green/native threading. This is just a bare-bones enough solution for
5 //! librustdoc, it is not production quality at all.
6
7 #![allow(non_camel_case_types)]
8 #![allow(nonstandard_style)]
9
10 use std::io;
11 use std::path::Path;
12
13 cfg_if! {
14     if #[cfg(unix)] {
15         use std::ffi::{CString, OsStr};
16         use std::os::unix::prelude::*;
17
18         #[cfg(any(target_os = "linux", target_os = "android"))]
19         mod os {
20             #[repr(C)]
21             pub struct flock {
22                 pub l_type: libc::c_short,
23                 pub l_whence: libc::c_short,
24                 pub l_start: libc::off_t,
25                 pub l_len: libc::off_t,
26                 pub l_pid: libc::pid_t,
27
28                 // not actually here, but brings in line with freebsd
29                 pub l_sysid: libc::c_int,
30             }
31         }
32
33         #[cfg(target_os = "freebsd")]
34         mod os {
35             #[repr(C)]
36             pub struct flock {
37                 pub l_start: libc::off_t,
38                 pub l_len: libc::off_t,
39                 pub l_pid: libc::pid_t,
40                 pub l_type: libc::c_short,
41                 pub l_whence: libc::c_short,
42                 pub l_sysid: libc::c_int,
43             }
44         }
45
46         #[cfg(any(target_os = "dragonfly",
47                   target_os = "netbsd",
48                   target_os = "openbsd"))]
49         mod os {
50             #[repr(C)]
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
58                 // not actually here, but brings in line with freebsd
59                 pub l_sysid: libc::c_int,
60             }
61         }
62
63         #[cfg(target_os = "haiku")]
64         mod os {
65             #[repr(C)]
66             pub struct flock {
67                 pub l_type: libc::c_short,
68                 pub l_whence: libc::c_short,
69                 pub l_start: libc::off_t,
70                 pub l_len: libc::off_t,
71                 pub l_pid: libc::pid_t,
72
73                 // not actually here, but brings in line with freebsd
74                 pub l_sysid: libc::c_int,
75             }
76         }
77
78         #[cfg(any(target_os = "macos", target_os = "ios"))]
79         mod os {
80             #[repr(C)]
81             pub struct flock {
82                 pub l_start: libc::off_t,
83                 pub l_len: libc::off_t,
84                 pub l_pid: libc::pid_t,
85                 pub l_type: libc::c_short,
86                 pub l_whence: libc::c_short,
87
88                 // not actually here, but brings in line with freebsd
89                 pub l_sysid: libc::c_int,
90             }
91         }
92
93         #[cfg(target_os = "solaris")]
94         mod os {
95             #[repr(C)]
96             pub struct flock {
97                 pub l_type: libc::c_short,
98                 pub l_whence: libc::c_short,
99                 pub l_start: libc::off_t,
100                 pub l_len: libc::off_t,
101                 pub l_sysid: libc::c_int,
102                 pub l_pid: libc::pid_t,
103             }
104         }
105
106         #[derive(Debug)]
107         pub struct Lock {
108             fd: libc::c_int,
109         }
110
111         impl Lock {
112             pub fn new(p: &Path,
113                        wait: bool,
114                        create: bool,
115                        exclusive: bool)
116                        -> io::Result<Lock> {
117                 let os: &OsStr = p.as_ref();
118                 let buf = CString::new(os.as_bytes()).unwrap();
119                 let open_flags = if create {
120                     libc::O_RDWR | libc::O_CREAT
121                 } else {
122                     libc::O_RDWR
123                 };
124
125                 let fd = unsafe {
126                     libc::open(buf.as_ptr(), open_flags,
127                                libc::S_IRWXU as libc::c_int)
128                 };
129
130                 if fd < 0 {
131                     return Err(io::Error::last_os_error());
132                 }
133
134                 let lock_type = if exclusive {
135                     libc::F_WRLCK as libc::c_short
136                 } else {
137                     libc::F_RDLCK as libc::c_short
138                 };
139
140                 let flock = os::flock {
141                     l_start: 0,
142                     l_len: 0,
143                     l_pid: 0,
144                     l_whence: libc::SEEK_SET as libc::c_short,
145                     l_type: lock_type,
146                     l_sysid: 0,
147                 };
148                 let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
149                 let ret = unsafe {
150                     libc::fcntl(fd, cmd, &flock)
151                 };
152                 if ret == -1 {
153                     let err = io::Error::last_os_error();
154                     unsafe { libc::close(fd); }
155                     Err(err)
156                 } else {
157                     Ok(Lock { fd })
158                 }
159             }
160         }
161
162         impl Drop for Lock {
163             fn drop(&mut self) {
164                 let flock = os::flock {
165                     l_start: 0,
166                     l_len: 0,
167                     l_pid: 0,
168                     l_whence: libc::SEEK_SET as libc::c_short,
169                     l_type: libc::F_UNLCK as libc::c_short,
170                     l_sysid: 0,
171                 };
172                 unsafe {
173                     libc::fcntl(self.fd, libc::F_SETLK, &flock);
174                     libc::close(self.fd);
175                 }
176             }
177         }
178     } else if #[cfg(windows)] {
179         use std::mem;
180         use std::os::windows::prelude::*;
181         use std::os::windows::raw::HANDLE;
182         use std::fs::{File, OpenOptions};
183         use std::os::raw::{c_ulong, c_int};
184
185         type DWORD = c_ulong;
186         type BOOL = c_int;
187         type ULONG_PTR = usize;
188
189         type LPOVERLAPPED = *mut OVERLAPPED;
190         const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x0000_0002;
191         const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x0000_0001;
192
193         const FILE_SHARE_DELETE: DWORD = 0x4;
194         const FILE_SHARE_READ: DWORD = 0x1;
195         const FILE_SHARE_WRITE: DWORD = 0x2;
196
197         #[repr(C)]
198         struct OVERLAPPED {
199             Internal: ULONG_PTR,
200             InternalHigh: ULONG_PTR,
201             Offset: DWORD,
202             OffsetHigh: DWORD,
203             hEvent: HANDLE,
204         }
205
206         extern "system" {
207             fn LockFileEx(hFile: HANDLE,
208                           dwFlags: DWORD,
209                           dwReserved: DWORD,
210                           nNumberOfBytesToLockLow: DWORD,
211                           nNumberOfBytesToLockHigh: DWORD,
212                           lpOverlapped: LPOVERLAPPED) -> BOOL;
213         }
214
215         #[derive(Debug)]
216         pub struct Lock {
217             _file: File,
218         }
219
220         impl Lock {
221             pub fn new(p: &Path,
222                        wait: bool,
223                        create: bool,
224                        exclusive: bool)
225                        -> io::Result<Lock> {
226                 assert!(p.parent().unwrap().exists(),
227                     "Parent directory of lock-file must exist: {}",
228                     p.display());
229
230                 let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
231
232                 let mut open_options = OpenOptions::new();
233                 open_options.read(true)
234                             .share_mode(share_mode);
235
236                 if create {
237                     open_options.create(true)
238                                 .write(true);
239                 }
240
241                 debug!("attempting to open lock file `{}`", p.display());
242                 let file = match open_options.open(p) {
243                     Ok(file) => {
244                         debug!("lock file opened successfully");
245                         file
246                     }
247                     Err(err) => {
248                         debug!("error opening lock file: {}", err);
249                         return Err(err)
250                     }
251                 };
252
253                 let ret = unsafe {
254                     let mut overlapped: OVERLAPPED = mem::zeroed();
255
256                     let mut dwFlags = 0;
257                     if !wait {
258                         dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
259                     }
260
261                     if exclusive {
262                         dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
263                     }
264
265                     debug!("attempting to acquire lock on lock file `{}`",
266                            p.display());
267                     LockFileEx(file.as_raw_handle(),
268                                dwFlags,
269                                0,
270                                0xFFFF_FFFF,
271                                0xFFFF_FFFF,
272                                &mut overlapped)
273                 };
274                 if ret == 0 {
275                     let err = io::Error::last_os_error();
276                     debug!("failed acquiring file lock: {}", err);
277                     Err(err)
278                 } else {
279                     debug!("successfully acquired lock");
280                     Ok(Lock { _file: file })
281                 }
282             }
283         }
284
285         // Note that we don't need a Drop impl on the Windows: The file is unlocked
286         // automatically when it's closed.
287     } else {
288         #[derive(Debug)]
289         pub struct Lock(());
290
291         impl Lock {
292             pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool)
293                 -> io::Result<Lock>
294             {
295                 let msg = "file locks not supported on this platform";
296                 Err(io::Error::new(io::ErrorKind::Other, msg))
297             }
298         }
299     }
300 }
301
302 impl Lock {
303     pub fn panicking_new(p: &Path,
304                          wait: bool,
305                          create: bool,
306                          exclusive: bool)
307                          -> Lock {
308         Lock::new(p, wait, create, exclusive).unwrap_or_else(|err| {
309             panic!("could not lock `{}`: {}", p.display(), err);
310         })
311     }
312 }