1 use std
::fs
::{File, OpenOptions}
;
4 use std
::os
::unix
::prelude
::*;
13 pub fn new(p
: &Path
, wait
: bool
, create
: bool
, exclusive
: bool
) -> io
::Result
<Lock
> {
14 let file
= OpenOptions
::new()
18 .mode(libc
::S_IRWXU
as u32)
21 let lock_type
= if exclusive { libc::F_WRLCK }
else { libc::F_RDLCK }
;
23 let mut flock
: libc
::flock
= unsafe { mem::zeroed() }
;
24 #[cfg(not(all(target_os = "hurd", target_arch = "x86")))]
26 flock
.l_type
= lock_type
as libc
::c_short
;
27 flock
.l_whence
= libc
::SEEK_SET
as libc
::c_short
;
29 #[cfg(all(target_os = "hurd", target_arch = "x86"))]
31 flock
.l_type
= lock_type
as libc
::c_int
;
32 flock
.l_whence
= libc
::SEEK_SET
as libc
::c_int
;
37 let cmd
= if wait { libc::F_SETLKW }
else { libc::F_SETLK }
;
38 let ret
= unsafe { libc::fcntl(file.as_raw_fd(), cmd, &flock) }
;
39 if ret
== -1 { Err(io::Error::last_os_error()) }
else { Ok(Lock { file }
) }
42 pub fn error_unsupported(err
: &io
::Error
) -> bool
{
43 matches
!(err
.raw_os_error(), Some(libc
::ENOTSUP
) | Some(libc
::ENOSYS
))
49 let mut flock
: libc
::flock
= unsafe { mem::zeroed() }
;
50 #[cfg(not(all(target_os = "hurd", target_arch = "x86")))]
52 flock
.l_type
= libc
::F_UNLCK
as libc
::c_short
;
53 flock
.l_whence
= libc
::SEEK_SET
as libc
::c_short
;
55 #[cfg(all(target_os = "hurd", target_arch = "x86"))]
57 flock
.l_type
= libc
::F_UNLCK
as libc
::c_int
;
58 flock
.l_whence
= libc
::SEEK_SET
as libc
::c_int
;
64 libc
::fcntl(self.file
.as_raw_fd(), libc
::F_SETLK
, &flock
);