pub trait Leasing: AsRawFd + Seek + FileExt
{
#[inline(always)]
fn acquire_lease(&self, lease: Lease) -> bool
{
let result = unsafe { fcntl(self.as_raw_fd(), F_SETLEASE, lease as i32) };
if likely!(result == 0)
{
true
}
else if likely!(result == -1)
{
match errno().0
{
EINTR => false,
EBADF => panic!("fd is not an open file descriptor"),
EINVAL => panic!("operation is invalid"),
unexpected @ _ => panic!("Unexpected error {} from fcntl()", unexpected)
}
}
else
{
unreachable_code(format_args!("Unexpected result {} from fcntl()", result))
}
}
#[inline(always)]
fn release_lease(&self) -> bool
{
let result = unsafe { fcntl(self.as_raw_fd(), F_SETLEASE, F_UNLCK) };
if likely!(result == 0)
{
true
}
else if likely!(result == -1)
{
match errno().0
{
EINTR => false,
EBADF => panic!("fd is not an open file descriptor"),
EINVAL => panic!("operation is invalid"),
unexpected @ _ => panic!("Unexpected error {} from fcntl()", unexpected)
}
}
else
{
unreachable_code(format_args!("Unexpected result {} from fcntl()", result))
}
}
#[inline(always)]
fn get_current_lease(&self) -> Result<Option<Lease>, ()>
{
use self::Lease::*;
match unsafe { fcntl(self.as_raw_fd(), F_GETLEASE) }
{
F_RDLCK => Ok(Some(Read)),
F_WRLCK => Ok(Some(Write)),
F_UNLCK => Ok(None),
-1 => match errno().0
{
EINTR => Err(()),
EBADF => panic!("fd is not an open file descriptor"),
EINVAL => panic!("operation is invalid"),
unexpected @ _ => panic!("Unexpected error {} from fcntl()", unexpected)
},
result @ _ => panic!("Unexpected result {} from fcntl()", result)
}
}
}