]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/flock.rs
Disable wasm32 features on emscripten
[rust.git] / src / librustc_data_structures / flock.rs
1 // Copyright 2014-2015 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 #![allow(nonstandard_style)]
19
20 use std::io;
21 use std::path::Path;
22
23 cfg_if! {
24     if #[cfg(unix)] {
25         use std::ffi::{CString, OsStr};
26         use std::os::unix::prelude::*;
27         use libc;
28
29         #[cfg(any(target_os = "linux", target_os = "android"))]
30         mod os {
31             use libc;
32
33             #[repr(C)]
34             pub struct flock {
35                 pub l_type: libc::c_short,
36                 pub l_whence: libc::c_short,
37                 pub l_start: libc::off_t,
38                 pub l_len: libc::off_t,
39                 pub l_pid: libc::pid_t,
40
41                 // not actually here, but brings in line with freebsd
42                 pub l_sysid: libc::c_int,
43             }
44
45             pub const F_RDLCK: libc::c_short = 0;
46             pub const F_WRLCK: libc::c_short = 1;
47             pub const F_UNLCK: libc::c_short = 2;
48             pub const F_SETLK: libc::c_int = 6;
49             pub const F_SETLKW: libc::c_int = 7;
50         }
51
52         #[cfg(target_os = "freebsd")]
53         mod os {
54             use libc;
55
56             #[repr(C)]
57             pub struct flock {
58                 pub l_start: libc::off_t,
59                 pub l_len: libc::off_t,
60                 pub l_pid: libc::pid_t,
61                 pub l_type: libc::c_short,
62                 pub l_whence: libc::c_short,
63                 pub l_sysid: libc::c_int,
64             }
65
66             pub const F_RDLCK: libc::c_short = 1;
67             pub const F_UNLCK: libc::c_short = 2;
68             pub const F_WRLCK: libc::c_short = 3;
69             pub const F_SETLK: libc::c_int = 12;
70             pub const F_SETLKW: libc::c_int = 13;
71         }
72
73         #[cfg(any(target_os = "dragonfly",
74                   target_os = "bitrig",
75                   target_os = "netbsd",
76                   target_os = "openbsd"))]
77         mod os {
78             use libc;
79
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             pub const F_RDLCK: libc::c_short = 1;
93             pub const F_UNLCK: libc::c_short = 2;
94             pub const F_WRLCK: libc::c_short = 3;
95             pub const F_SETLK: libc::c_int = 8;
96             pub const F_SETLKW: libc::c_int = 9;
97         }
98
99         #[cfg(target_os = "haiku")]
100         mod os {
101             use libc;
102
103             #[repr(C)]
104             pub struct flock {
105                 pub l_type: libc::c_short,
106                 pub l_whence: libc::c_short,
107                 pub l_start: libc::off_t,
108                 pub l_len: libc::off_t,
109                 pub l_pid: libc::pid_t,
110
111                 // not actually here, but brings in line with freebsd
112                 pub l_sysid: libc::c_int,
113             }
114
115             pub const F_RDLCK: libc::c_short = 0x0040;
116             pub const F_UNLCK: libc::c_short = 0x0200;
117             pub const F_WRLCK: libc::c_short = 0x0400;
118             pub const F_SETLK: libc::c_int = 0x0080;
119             pub const F_SETLKW: libc::c_int = 0x0100;
120         }
121
122         #[cfg(any(target_os = "macos", target_os = "ios"))]
123         mod os {
124             use libc;
125
126             #[repr(C)]
127             pub struct flock {
128                 pub l_start: libc::off_t,
129                 pub l_len: libc::off_t,
130                 pub l_pid: libc::pid_t,
131                 pub l_type: libc::c_short,
132                 pub l_whence: libc::c_short,
133
134                 // not actually here, but brings in line with freebsd
135                 pub l_sysid: libc::c_int,
136             }
137
138             pub const F_RDLCK: libc::c_short = 1;
139             pub const F_UNLCK: libc::c_short = 2;
140             pub const F_WRLCK: libc::c_short = 3;
141             pub const F_SETLK: libc::c_int = 8;
142             pub const F_SETLKW: libc::c_int = 9;
143         }
144
145         #[cfg(target_os = "solaris")]
146         mod os {
147             use libc;
148
149             #[repr(C)]
150             pub struct flock {
151                 pub l_type: libc::c_short,
152                 pub l_whence: libc::c_short,
153                 pub l_start: libc::off_t,
154                 pub l_len: libc::off_t,
155                 pub l_sysid: libc::c_int,
156                 pub l_pid: libc::pid_t,
157             }
158
159             pub const F_RDLCK: libc::c_short = 1;
160             pub const F_WRLCK: libc::c_short = 2;
161             pub const F_UNLCK: libc::c_short = 3;
162             pub const F_SETLK: libc::c_int = 6;
163             pub const F_SETLKW: libc::c_int = 7;
164         }
165
166         #[derive(Debug)]
167         pub struct Lock {
168             fd: libc::c_int,
169         }
170
171         impl Lock {
172             pub fn new(p: &Path,
173                        wait: bool,
174                        create: bool,
175                        exclusive: bool)
176                        -> io::Result<Lock> {
177                 let os: &OsStr = p.as_ref();
178                 let buf = CString::new(os.as_bytes()).unwrap();
179                 let open_flags = if create {
180                     libc::O_RDWR | libc::O_CREAT
181                 } else {
182                     libc::O_RDWR
183                 };
184
185                 let fd = unsafe {
186                     libc::open(buf.as_ptr(), open_flags,
187                                libc::S_IRWXU as libc::c_int)
188                 };
189
190                 if fd < 0 {
191                     return Err(io::Error::last_os_error());
192                 }
193
194                 let lock_type = if exclusive {
195                     os::F_WRLCK
196                 } else {
197                     os::F_RDLCK
198                 };
199
200                 let flock = os::flock {
201                     l_start: 0,
202                     l_len: 0,
203                     l_pid: 0,
204                     l_whence: libc::SEEK_SET as libc::c_short,
205                     l_type: lock_type,
206                     l_sysid: 0,
207                 };
208                 let cmd = if wait { os::F_SETLKW } else { os::F_SETLK };
209                 let ret = unsafe {
210                     libc::fcntl(fd, cmd, &flock)
211                 };
212                 if ret == -1 {
213                     let err = io::Error::last_os_error();
214                     unsafe { libc::close(fd); }
215                     Err(err)
216                 } else {
217                     Ok(Lock { fd: fd })
218                 }
219             }
220         }
221
222         impl Drop for Lock {
223             fn drop(&mut self) {
224                 let flock = os::flock {
225                     l_start: 0,
226                     l_len: 0,
227                     l_pid: 0,
228                     l_whence: libc::SEEK_SET as libc::c_short,
229                     l_type: os::F_UNLCK,
230                     l_sysid: 0,
231                 };
232                 unsafe {
233                     libc::fcntl(self.fd, os::F_SETLK, &flock);
234                     libc::close(self.fd);
235                 }
236             }
237         }
238     } else if #[cfg(windows)] {
239         use std::mem;
240         use std::os::windows::prelude::*;
241         use std::os::windows::raw::HANDLE;
242         use std::fs::{File, OpenOptions};
243         use std::os::raw::{c_ulong, c_int};
244
245         type DWORD = c_ulong;
246         type BOOL = c_int;
247         type ULONG_PTR = usize;
248
249         type LPOVERLAPPED = *mut OVERLAPPED;
250         const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x0000_0002;
251         const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x0000_0001;
252
253         const FILE_SHARE_DELETE: DWORD = 0x4;
254         const FILE_SHARE_READ: DWORD = 0x1;
255         const FILE_SHARE_WRITE: DWORD = 0x2;
256
257         #[repr(C)]
258         struct OVERLAPPED {
259             Internal: ULONG_PTR,
260             InternalHigh: ULONG_PTR,
261             Offset: DWORD,
262             OffsetHigh: DWORD,
263             hEvent: HANDLE,
264         }
265
266         extern "system" {
267             fn LockFileEx(hFile: HANDLE,
268                           dwFlags: DWORD,
269                           dwReserved: DWORD,
270                           nNumberOfBytesToLockLow: DWORD,
271                           nNumberOfBytesToLockHigh: DWORD,
272                           lpOverlapped: LPOVERLAPPED) -> BOOL;
273         }
274
275         #[derive(Debug)]
276         pub struct Lock {
277             _file: File,
278         }
279
280         impl Lock {
281             pub fn new(p: &Path,
282                        wait: bool,
283                        create: bool,
284                        exclusive: bool)
285                        -> io::Result<Lock> {
286                 assert!(p.parent().unwrap().exists(),
287                     "Parent directory of lock-file must exist: {}",
288                     p.display());
289
290                 let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
291
292                 let mut open_options = OpenOptions::new();
293                 open_options.read(true)
294                             .share_mode(share_mode);
295
296                 if create {
297                     open_options.create(true)
298                                 .write(true);
299                 }
300
301                 debug!("Attempting to open lock file `{}`", p.display());
302                 let file = match open_options.open(p) {
303                     Ok(file) => {
304                         debug!("Lock file opened successfully");
305                         file
306                     }
307                     Err(err) => {
308                         debug!("Error opening lock file: {}", err);
309                         return Err(err)
310                     }
311                 };
312
313                 let ret = unsafe {
314                     let mut overlapped: OVERLAPPED = mem::zeroed();
315
316                     let mut dwFlags = 0;
317                     if !wait {
318                         dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
319                     }
320
321                     if exclusive {
322                         dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
323                     }
324
325                     debug!("Attempting to acquire lock on lock file `{}`",
326                            p.display());
327                     LockFileEx(file.as_raw_handle(),
328                                dwFlags,
329                                0,
330                                0xFFFF_FFFF,
331                                0xFFFF_FFFF,
332                                &mut overlapped)
333                 };
334                 if ret == 0 {
335                     let err = io::Error::last_os_error();
336                     debug!("Failed acquiring file lock: {}", err);
337                     Err(err)
338                 } else {
339                     debug!("Successfully acquired lock.");
340                     Ok(Lock { _file: file })
341                 }
342             }
343         }
344
345         // Note that we don't need a Drop impl on the Windows: The file is unlocked
346         // automatically when it's closed.
347     } else {
348         #[derive(Debug)]
349         pub struct Lock(());
350
351         impl Lock {
352             pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool)
353                 -> io::Result<Lock>
354             {
355                 let msg = "file locks not supported on this platform";
356                 Err(io::Error::new(io::ErrorKind::Other, msg))
357             }
358         }
359     }
360 }
361
362 impl Lock {
363     pub fn panicking_new(p: &Path,
364                          wait: bool,
365                          create: bool,
366                          exclusive: bool)
367                          -> Lock {
368         Lock::new(p, wait, create, exclusive).unwrap_or_else(|err| {
369             panic!("could not lock `{}`: {}", p.display(), err);
370         })
371     }
372 }