|
2 | 2 |
|
3 | 3 | use std::borrow::Cow; |
4 | 4 | use std::fs::{ |
5 | | - DirBuilder, File, FileType, OpenOptions, ReadDir, read_dir, remove_dir, remove_file, rename, |
| 5 | + DirBuilder, File, FileType, OpenOptions, ReadDir, TryLockError, read_dir, remove_dir, |
| 6 | + remove_file, rename, |
6 | 7 | }; |
7 | 8 | use std::io::{self, ErrorKind, Read, Seek, SeekFrom, Write}; |
8 | 9 | use std::path::{Path, PathBuf}; |
@@ -89,103 +90,27 @@ impl UnixFileDescription for FileHandle { |
89 | 90 | communicate_allowed: bool, |
90 | 91 | op: FlockOp, |
91 | 92 | ) -> InterpResult<'tcx, io::Result<()>> { |
92 | | - // cfg(bootstrap) |
93 | | - macro_rules! cfg_select_dispatch { |
94 | | - ($($tokens:tt)*) => { |
95 | | - #[cfg(bootstrap)] |
96 | | - cfg_match! { $($tokens)* } |
97 | | - |
98 | | - #[cfg(not(bootstrap))] |
99 | | - cfg_select! { $($tokens)* } |
100 | | - }; |
101 | | - } |
102 | | - |
103 | 93 | assert!(communicate_allowed, "isolation should have prevented even opening a file"); |
104 | | - cfg_select_dispatch! { |
105 | | - all(target_family = "unix", not(target_os = "solaris")) => { |
106 | | - use std::os::fd::AsRawFd; |
107 | | - |
108 | | - use FlockOp::*; |
109 | | - // We always use non-blocking call to prevent interpreter from being blocked |
110 | | - let (host_op, lock_nb) = match op { |
111 | | - SharedLock { nonblocking } => (libc::LOCK_SH | libc::LOCK_NB, nonblocking), |
112 | | - ExclusiveLock { nonblocking } => (libc::LOCK_EX | libc::LOCK_NB, nonblocking), |
113 | | - Unlock => (libc::LOCK_UN, false), |
114 | | - }; |
115 | 94 |
|
116 | | - let fd = self.file.as_raw_fd(); |
117 | | - let ret = unsafe { libc::flock(fd, host_op) }; |
118 | | - let res = match ret { |
119 | | - 0 => Ok(()), |
120 | | - -1 => { |
121 | | - let err = io::Error::last_os_error(); |
122 | | - if !lock_nb && err.kind() == io::ErrorKind::WouldBlock { |
123 | | - throw_unsup_format!("blocking `flock` is not currently supported"); |
124 | | - } |
125 | | - Err(err) |
126 | | - } |
127 | | - ret => panic!("Unexpected return value from flock: {ret}"), |
128 | | - }; |
129 | | - interp_ok(res) |
| 95 | + use FlockOp::*; |
| 96 | + // We must not block the interpreter loop, so we always `try_lock`. |
| 97 | + let (res, nonblocking) = match op { |
| 98 | + SharedLock { nonblocking } => (self.file.try_lock_shared(), nonblocking), |
| 99 | + ExclusiveLock { nonblocking } => (self.file.try_lock(), nonblocking), |
| 100 | + Unlock => { |
| 101 | + return interp_ok(self.file.unlock()); |
130 | 102 | } |
131 | | - target_family = "windows" => { |
132 | | - use std::os::windows::io::AsRawHandle; |
133 | | - |
134 | | - use windows_sys::Win32::Foundation::{ |
135 | | - ERROR_IO_PENDING, ERROR_LOCK_VIOLATION, FALSE, TRUE, |
136 | | - }; |
137 | | - use windows_sys::Win32::Storage::FileSystem::{ |
138 | | - LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY, LockFileEx, UnlockFile, |
139 | | - }; |
140 | | - |
141 | | - let fh = self.file.as_raw_handle(); |
142 | | - |
143 | | - use FlockOp::*; |
144 | | - let (ret, lock_nb) = match op { |
145 | | - SharedLock { nonblocking } | ExclusiveLock { nonblocking } => { |
146 | | - // We always use non-blocking call to prevent interpreter from being blocked |
147 | | - let mut flags = LOCKFILE_FAIL_IMMEDIATELY; |
148 | | - if matches!(op, ExclusiveLock { .. }) { |
149 | | - flags |= LOCKFILE_EXCLUSIVE_LOCK; |
150 | | - } |
151 | | - let ret = unsafe { LockFileEx(fh, flags, 0, !0, !0, &mut std::mem::zeroed()) }; |
152 | | - (ret, nonblocking) |
153 | | - } |
154 | | - Unlock => { |
155 | | - let ret = unsafe { UnlockFile(fh, 0, 0, !0, !0) }; |
156 | | - (ret, false) |
157 | | - } |
158 | | - }; |
| 103 | + }; |
159 | 104 |
|
160 | | - let res = match ret { |
161 | | - TRUE => Ok(()), |
162 | | - FALSE => { |
163 | | - let mut err = io::Error::last_os_error(); |
164 | | - // This only runs on Windows hosts so we can use `raw_os_error`. |
165 | | - // We have to be careful not to forward that error code to target code. |
166 | | - let code: u32 = err.raw_os_error().unwrap().try_into().unwrap(); |
167 | | - if matches!(code, ERROR_IO_PENDING | ERROR_LOCK_VIOLATION) { |
168 | | - if lock_nb { |
169 | | - // The io error mapping does not know about these error codes, |
170 | | - // so we translate it to `WouldBlock` manually. |
171 | | - let desc = format!("LockFileEx wouldblock error: {err}"); |
172 | | - err = io::Error::new(io::ErrorKind::WouldBlock, desc); |
173 | | - } else { |
174 | | - throw_unsup_format!("blocking `flock` is not currently supported"); |
175 | | - } |
176 | | - } |
177 | | - Err(err) |
178 | | - } |
179 | | - _ => panic!("Unexpected return value: {ret}"), |
180 | | - }; |
181 | | - interp_ok(res) |
182 | | - } |
183 | | - _ => { |
184 | | - let _ = op; |
185 | | - throw_unsup_format!( |
186 | | - "flock is supported only on UNIX (except Solaris) and Windows hosts" |
187 | | - ); |
188 | | - } |
| 105 | + match res { |
| 106 | + Ok(()) => interp_ok(Ok(())), |
| 107 | + Err(TryLockError::Error(err)) => interp_ok(Err(err)), |
| 108 | + Err(TryLockError::WouldBlock) => |
| 109 | + if nonblocking { |
| 110 | + interp_ok(Err(ErrorKind::WouldBlock.into())) |
| 111 | + } else { |
| 112 | + throw_unsup_format!("blocking `flock` is not currently supported"); |
| 113 | + }, |
189 | 114 | } |
190 | 115 | } |
191 | 116 | } |
|
0 commit comments