diff options
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/fcntl.rs | 32 |
2 files changed, 33 insertions, 1 deletions
@@ -19,7 +19,7 @@ eventfd = [] execvpe = [] [dependencies] -libc = "0.1.8" +libc = "0.1.10" bitflags = "0.1.1" [dev-dependencies] diff --git a/src/fcntl.rs b/src/fcntl.rs index 7893addb..1bc11c74 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -11,6 +11,8 @@ pub use self::ffi::flock; mod ffi { pub use libc::{open, fcntl}; pub use self::os::*; + pub use libc::funcs::bsd44::flock as libc_flock; + pub use libc::consts::os::bsd44::{LOCK_SH, LOCK_EX, LOCK_NB, LOCK_UN}; #[cfg(any(target_os = "linux", target_os = "android"))] mod os { @@ -128,6 +130,36 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> { Ok(res) } +pub enum FlockArg { + LockShared, + LockExclusive, + Unlock, + LockSharedNonblock, + LockExclusiveNonblock, + UnlockNonblock, +} + +pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> { + use self::FlockArg::*; + + let res = unsafe { + match arg { + LockShared => ffi::libc_flock(fd, ffi::LOCK_SH), + LockExclusive => ffi::libc_flock(fd, ffi::LOCK_EX), + Unlock => ffi::libc_flock(fd, ffi::LOCK_UN), + LockSharedNonblock => ffi::libc_flock(fd, ffi::LOCK_SH | ffi::LOCK_NB), + LockExclusiveNonblock => ffi::libc_flock(fd, ffi::LOCK_EX | ffi::LOCK_NB), + UnlockNonblock => ffi::libc_flock(fd, ffi::LOCK_UN | ffi::LOCK_NB), + } + }; + + if res < 0 { + return Err(Error::Sys(Errno::last())); + } + + Ok(()) +} + #[cfg(any(target_os = "linux", target_os = "android"))] mod consts { use libc::c_int; |