possum/sys/flock/
unix.rs

1//! Implement file locking for systems that lack open file description segment locking but have flock.
2
3pub use nix::fcntl::FlockArg;
4pub use nix::fcntl::FlockArg::*;
5
6use super::*;
7
8pub trait Flock {
9    fn flock(&self, arg: FlockArg) -> io::Result<bool>;
10}
11
12impl Flock for File {
13    /// Locks a segment that spans the maximum possible range of offsets.
14    fn flock(&self, arg: FlockArg) -> io::Result<bool> {
15        match nix::fcntl::flock(self.as_raw_fd(), arg) {
16            Ok(()) => Ok(true),
17            Err(errno) if errno == nix::Error::EWOULDBLOCK => Ok(false),
18            Err(errno) => Err(std::io::Error::from_raw_os_error(errno as i32)),
19        }
20    }
21}