@@ -178,92 +178,91 @@ impl UnixFileDescription for FileHandle {
178178 op : FlockOp ,
179179 ) -> InterpResult < ' tcx , io:: Result < ( ) > > {
180180 assert ! ( communicate_allowed, "isolation should have prevented even opening a file" ) ;
181- # [ cfg ( target_family = "unix" ) ]
182- {
183- use std:: os:: fd:: AsRawFd ;
184-
185- use FlockOp :: * ;
186- // We always use non-blocking call to prevent interpreter from being blocked
187- let ( host_op, lock_nb) = match op {
188- SharedLock { nonblocking } => ( libc:: LOCK_SH | libc:: LOCK_NB , nonblocking) ,
189- ExclusiveLock { nonblocking } => ( libc:: LOCK_EX | libc:: LOCK_NB , nonblocking) ,
190- Unlock => ( libc:: LOCK_UN , false ) ,
191- } ;
181+ cfg_match ! {
182+ all ( target_family = "unix" , not ( target_os = "solaris" ) ) => {
183+ use std:: os:: fd:: AsRawFd ;
184+
185+ use FlockOp :: * ;
186+ // We always use non-blocking call to prevent interpreter from being blocked
187+ let ( host_op, lock_nb) = match op {
188+ SharedLock { nonblocking } => ( libc:: LOCK_SH | libc:: LOCK_NB , nonblocking) ,
189+ ExclusiveLock { nonblocking } => ( libc:: LOCK_EX | libc:: LOCK_NB , nonblocking) ,
190+ Unlock => ( libc:: LOCK_UN , false ) ,
191+ } ;
192192
193- let fd = self . file . as_raw_fd ( ) ;
194- let ret = unsafe { libc:: flock ( fd, host_op) } ;
195- let res = match ret {
196- 0 => Ok ( ( ) ) ,
197- -1 => {
198- let err = io:: Error :: last_os_error ( ) ;
199- if !lock_nb && err. kind ( ) == io:: ErrorKind :: WouldBlock {
200- throw_unsup_format ! ( "blocking `flock` is not currently supported" ) ;
193+ let fd = self . file. as_raw_fd( ) ;
194+ let ret = unsafe { libc:: flock( fd, host_op) } ;
195+ let res = match ret {
196+ 0 => Ok ( ( ) ) ,
197+ -1 => {
198+ let err = io:: Error :: last_os_error( ) ;
199+ if !lock_nb && err. kind( ) == io:: ErrorKind :: WouldBlock {
200+ throw_unsup_format!( "blocking `flock` is not currently supported" ) ;
201+ }
202+ Err ( err)
201203 }
202- Err ( err)
203- }
204- ret => panic ! ( "Unexpected return value from flock: {ret}" ) ,
205- } ;
206- interp_ok ( res)
207- }
208-
209- #[ cfg( target_family = "windows" ) ]
210- {
211- use std:: os:: windows:: io:: AsRawHandle ;
204+ ret => panic!( "Unexpected return value from flock: {ret}" ) ,
205+ } ;
206+ interp_ok( res)
207+ }
208+ target_family = "windows" => {
209+ use std:: os:: windows:: io:: AsRawHandle ;
212210
213- use windows_sys:: Win32 :: Foundation :: {
214- ERROR_IO_PENDING , ERROR_LOCK_VIOLATION , FALSE , HANDLE , TRUE ,
215- } ;
216- use windows_sys:: Win32 :: Storage :: FileSystem :: {
217- LOCKFILE_EXCLUSIVE_LOCK , LOCKFILE_FAIL_IMMEDIATELY , LockFileEx , UnlockFile ,
218- } ;
211+ use windows_sys:: Win32 :: Foundation :: {
212+ ERROR_IO_PENDING , ERROR_LOCK_VIOLATION , FALSE , HANDLE , TRUE ,
213+ } ;
214+ use windows_sys:: Win32 :: Storage :: FileSystem :: {
215+ LOCKFILE_EXCLUSIVE_LOCK , LOCKFILE_FAIL_IMMEDIATELY , LockFileEx , UnlockFile ,
216+ } ;
219217
220- let fh = self . file . as_raw_handle ( ) as HANDLE ;
218+ let fh = self . file. as_raw_handle( ) as HANDLE ;
221219
222- use FlockOp :: * ;
223- let ( ret, lock_nb) = match op {
224- SharedLock { nonblocking } | ExclusiveLock { nonblocking } => {
225- // We always use non-blocking call to prevent interpreter from being blocked
226- let mut flags = LOCKFILE_FAIL_IMMEDIATELY ;
227- if matches ! ( op, ExclusiveLock { .. } ) {
228- flags |= LOCKFILE_EXCLUSIVE_LOCK ;
220+ use FlockOp :: * ;
221+ let ( ret, lock_nb) = match op {
222+ SharedLock { nonblocking } | ExclusiveLock { nonblocking } => {
223+ // We always use non-blocking call to prevent interpreter from being blocked
224+ let mut flags = LOCKFILE_FAIL_IMMEDIATELY ;
225+ if matches!( op, ExclusiveLock { .. } ) {
226+ flags |= LOCKFILE_EXCLUSIVE_LOCK ;
227+ }
228+ let ret = unsafe { LockFileEx ( fh, flags, 0 , !0 , !0 , & mut std:: mem:: zeroed( ) ) } ;
229+ ( ret, nonblocking)
229230 }
230- let ret = unsafe { LockFileEx ( fh, flags, 0 , !0 , !0 , & mut std:: mem:: zeroed ( ) ) } ;
231- ( ret, nonblocking)
232- }
233- Unlock => {
234- let ret = unsafe { UnlockFile ( fh, 0 , 0 , !0 , !0 ) } ;
235- ( ret, false )
236- }
237- } ;
231+ Unlock => {
232+ let ret = unsafe { UnlockFile ( fh, 0 , 0 , !0 , !0 ) } ;
233+ ( ret, false )
234+ }
235+ } ;
238236
239- let res = match ret {
240- TRUE => Ok ( ( ) ) ,
241- FALSE => {
242- let mut err = io:: Error :: last_os_error ( ) ;
243- // This only runs on Windows hosts so we can use `raw_os_error`.
244- // We have to be careful not to forward that error code to target code.
245- let code: u32 = err. raw_os_error ( ) . unwrap ( ) . try_into ( ) . unwrap ( ) ;
246- if matches ! ( code, ERROR_IO_PENDING | ERROR_LOCK_VIOLATION ) {
247- if lock_nb {
248- // The io error mapping does not know about these error codes,
249- // so we translate it to `WouldBlock` manually.
250- let desc = format ! ( "LockFileEx wouldblock error: {err}" ) ;
251- err = io:: Error :: new ( io:: ErrorKind :: WouldBlock , desc) ;
252- } else {
253- throw_unsup_format ! ( "blocking `flock` is not currently supported" ) ;
237+ let res = match ret {
238+ TRUE => Ok ( ( ) ) ,
239+ FALSE => {
240+ let mut err = io:: Error :: last_os_error( ) ;
241+ // This only runs on Windows hosts so we can use `raw_os_error`.
242+ // We have to be careful not to forward that error code to target code.
243+ let code: u32 = err. raw_os_error( ) . unwrap( ) . try_into( ) . unwrap( ) ;
244+ if matches!( code, ERROR_IO_PENDING | ERROR_LOCK_VIOLATION ) {
245+ if lock_nb {
246+ // The io error mapping does not know about these error codes,
247+ // so we translate it to `WouldBlock` manually.
248+ let desc = format!( "LockFileEx wouldblock error: {err}" ) ;
249+ err = io:: Error :: new( io:: ErrorKind :: WouldBlock , desc) ;
250+ } else {
251+ throw_unsup_format!( "blocking `flock` is not currently supported" ) ;
252+ }
254253 }
254+ Err ( err)
255255 }
256- Err ( err)
257- }
258- _ => panic ! ( "Unexpected return value: {ret}" ) ,
259- } ;
260- interp_ok ( res)
261- }
262-
263- #[ cfg( not( any( target_family = "unix" , target_family = "windows" ) ) ) ]
264- {
265- let _ = op;
266- compile_error ! ( "flock is supported only on UNIX and Windows hosts" ) ;
256+ _ => panic!( "Unexpected return value: {ret}" ) ,
257+ } ;
258+ interp_ok( res)
259+ }
260+ _ => {
261+ let _ = op;
262+ throw_unsup_format!(
263+ "flock is supported only on UNIX (except Solaris) and Windows hosts"
264+ ) ;
265+ }
267266 }
268267 }
269268}
0 commit comments