]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/flock.rs
Rollup merge of #67784 - Mark-Simulacrum:residual-pad-integral, r=dtolnay
[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::mem;
17         use std::os::unix::prelude::*;
18
19         #[derive(Debug)]
20         pub struct Lock {
21             fd: libc::c_int,
22         }
23
24         impl Lock {
25             pub fn new(p: &Path,
26                        wait: bool,
27                        create: bool,
28                        exclusive: bool)
29                        -> io::Result<Lock> {
30                 let os: &OsStr = p.as_ref();
31                 let buf = CString::new(os.as_bytes()).unwrap();
32                 let open_flags = if create {
33                     libc::O_RDWR | libc::O_CREAT
34                 } else {
35                     libc::O_RDWR
36                 };
37
38                 let fd = unsafe {
39                     libc::open(buf.as_ptr(), open_flags,
40                                libc::S_IRWXU as libc::c_int)
41                 };
42
43                 if fd < 0 {
44                     return Err(io::Error::last_os_error());
45                 }
46
47                 let lock_type = if exclusive {
48                     libc::F_WRLCK
49                 } else {
50                     libc::F_RDLCK
51                 };
52
53                 let mut flock: libc::flock = unsafe { mem::zeroed() };
54                 flock.l_type = lock_type as libc::c_short;
55                 flock.l_whence = libc::SEEK_SET as libc::c_short;
56                 flock.l_start = 0;
57                 flock.l_len = 0;
58
59                 let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
60                 let ret = unsafe {
61                     libc::fcntl(fd, cmd, &flock)
62                 };
63                 if ret == -1 {
64                     let err = io::Error::last_os_error();
65                     unsafe { libc::close(fd); }
66                     Err(err)
67                 } else {
68                     Ok(Lock { fd })
69                 }
70             }
71         }
72
73         impl Drop for Lock {
74             fn drop(&mut self) {
75                 let mut flock: libc::flock = unsafe { mem::zeroed() };
76                 flock.l_type = libc::F_UNLCK as libc::c_short;
77                 flock.l_whence = libc::SEEK_SET as libc::c_short;
78                 flock.l_start = 0;
79                 flock.l_len = 0;
80
81                 unsafe {
82                     libc::fcntl(self.fd, libc::F_SETLK, &flock);
83                     libc::close(self.fd);
84                 }
85             }
86         }
87     } else if #[cfg(windows)] {
88         use std::mem;
89         use std::os::windows::prelude::*;
90         use std::os::windows::raw::HANDLE;
91         use std::fs::{File, OpenOptions};
92         use std::os::raw::{c_ulong, c_int};
93
94         type DWORD = c_ulong;
95         type BOOL = c_int;
96         type ULONG_PTR = usize;
97
98         type LPOVERLAPPED = *mut OVERLAPPED;
99         const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x0000_0002;
100         const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x0000_0001;
101
102         const FILE_SHARE_DELETE: DWORD = 0x4;
103         const FILE_SHARE_READ: DWORD = 0x1;
104         const FILE_SHARE_WRITE: DWORD = 0x2;
105
106         #[repr(C)]
107         struct OVERLAPPED {
108             Internal: ULONG_PTR,
109             InternalHigh: ULONG_PTR,
110             Offset: DWORD,
111             OffsetHigh: DWORD,
112             hEvent: HANDLE,
113         }
114
115         extern "system" {
116             fn LockFileEx(hFile: HANDLE,
117                           dwFlags: DWORD,
118                           dwReserved: DWORD,
119                           nNumberOfBytesToLockLow: DWORD,
120                           nNumberOfBytesToLockHigh: DWORD,
121                           lpOverlapped: LPOVERLAPPED) -> BOOL;
122         }
123
124         #[derive(Debug)]
125         pub struct Lock {
126             _file: File,
127         }
128
129         impl Lock {
130             pub fn new(p: &Path,
131                        wait: bool,
132                        create: bool,
133                        exclusive: bool)
134                        -> io::Result<Lock> {
135                 assert!(p.parent().unwrap().exists(),
136                     "Parent directory of lock-file must exist: {}",
137                     p.display());
138
139                 let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
140
141                 let mut open_options = OpenOptions::new();
142                 open_options.read(true)
143                             .share_mode(share_mode);
144
145                 if create {
146                     open_options.create(true)
147                                 .write(true);
148                 }
149
150                 debug!("attempting to open lock file `{}`", p.display());
151                 let file = match open_options.open(p) {
152                     Ok(file) => {
153                         debug!("lock file opened successfully");
154                         file
155                     }
156                     Err(err) => {
157                         debug!("error opening lock file: {}", err);
158                         return Err(err)
159                     }
160                 };
161
162                 let ret = unsafe {
163                     let mut overlapped: OVERLAPPED = mem::zeroed();
164
165                     let mut dwFlags = 0;
166                     if !wait {
167                         dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
168                     }
169
170                     if exclusive {
171                         dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
172                     }
173
174                     debug!("attempting to acquire lock on lock file `{}`",
175                            p.display());
176                     LockFileEx(file.as_raw_handle(),
177                                dwFlags,
178                                0,
179                                0xFFFF_FFFF,
180                                0xFFFF_FFFF,
181                                &mut overlapped)
182                 };
183                 if ret == 0 {
184                     let err = io::Error::last_os_error();
185                     debug!("failed acquiring file lock: {}", err);
186                     Err(err)
187                 } else {
188                     debug!("successfully acquired lock");
189                     Ok(Lock { _file: file })
190                 }
191             }
192         }
193
194         // Note that we don't need a Drop impl on the Windows: The file is unlocked
195         // automatically when it's closed.
196     } else {
197         #[derive(Debug)]
198         pub struct Lock(());
199
200         impl Lock {
201             pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool)
202                 -> io::Result<Lock>
203             {
204                 let msg = "file locks not supported on this platform";
205                 Err(io::Error::new(io::ErrorKind::Other, msg))
206             }
207         }
208     }
209 }