pub unsafe fn raw_file_lock(
    f: &File,
    lock: Option<Lock>,
    off: usize,
    len: usize,
    wait: bool,
) -> io::Result<()> {
    if len == 0 {
        return Err(ErrorKind::InvalidInput.into());
    }

    let mut ov: OVERLAPPED = MaybeUninit::zeroed().assume_init();
    let mut s = ov.u.s_mut();
    s.Offset = (off & 0xffffffff) as DWORD;
    s.OffsetHigh = (off >> 16 >> 16) as DWORD;

    let lenlow = (len & 0xffffffff) as DWORD;
    let lenhigh = (len >> 16 >> 16) as DWORD;

    let rc = if let Some(lock) = lock {
        let mut flags = if wait { 0 } else { LOCKFILE_FAIL_IMMEDIATELY };
        if lock == Lock::Exclusive {
            flags |= LOCKFILE_EXCLUSIVE_LOCK;
        }
        LockFileEx(f.as_raw_handle(), flags, 0, lenlow, lenhigh, &mut ov)
    } else {
        UnlockFileEx(f.as_raw_handle(), 0, lenlow, lenhigh, &mut ov)
    };

    if rc == 0 {
        let e = Error::last_os_error();
        if e.raw_os_error() == Some(ERROR_LOCK_VIOLATION as i32) {
            Err(ErrorKind::WouldBlock.into())
        } else {
            Err(e)
        }
    } else {
        Ok(())
    }
}
/// Acquires and releases a file lock.
///
/// # Safety
///
/// When used to unlock, this does not guarantee that an exclusive lock is
/// already held.
Acquires and releases a file lock.
Acquires and releases a file lock .
pub unsafe fn raw_file_lock(
    f: &File,
    lock: Option<Lock>,
    off: usize,
    len: usize,
    wait: bool,
) -> io::Result<()> {
    if len == 0 {
        return Err(ErrorKind::InvalidInput.into());
    }
    let mut ov: OVERLAPPED = MaybeUninit::zeroed().assume_init();
    let mut s = ov.u.s_mut();
    s.Offset = (off & 0xffffffff) as DWORD;
    s.OffsetHigh = (off >> 16 >> 16) as DWORD;
    let lenlow = (len & 0xffffffff) as DWORD;
    let lenhigh = (len >> 16 >> 16) as DWORD;
    let rc = if let Some(lock) = lock {
        let mut flags = if wait { 0 } else { LOCKFILE_FAIL_IMMEDIATELY };
        if lock == Lock::Exclusive {
            flags |= LOCKFILE_EXCLUSIVE_LOCK;
        }
        LockFileEx(f.as_raw_handle(), flags, 0, lenlow, lenhigh, &mut ov)
    } else {
        UnlockFileEx(f.as_raw_handle(), 0, lenlow, lenhigh, &mut ov)
    };
    if rc == 0 {
        let e = Error::last_os_error();
        if e.raw_os_error() == Some(ERROR_LOCK_VIOLATION as i32) {
            Err(ErrorKind::WouldBlock.into())
        } else {
            Err(e)
        }
    } else {
        Ok(())
    }
}
pub unsafe fn raw_file_lock ( f : & File , lock : Option < Lock > , off : usize , len : usize , wait : bool , ) -> io :: Result < ( ) > { if len == 0 { return Err ( ErrorKind :: InvalidInput . into ( ) ) ; } let mut ov : OVERLAPPED = MaybeUninit :: zeroed ( ) . assume_init ( ) ; let mut s = ov . u . s_mut ( ) ; s . Offset = ( off & 0xffffffff ) as DWORD ; s . OffsetHigh = ( off >> 16 >> 16 ) as DWORD ; let lenlow = ( len & 0xffffffff ) as DWORD ; let lenhigh = ( len >> 16 >> 16 ) as DWORD ; let rc = if let Some ( lock ) = lock { let mut flags = if wait { 0 } else { LOCKFILE_FAIL_IMMEDIATELY } ; if lock == Lock :: Exclusive { flags |= LOCKFILE_EXCLUSIVE_LOCK ; } LockFileEx ( f . as_raw_handle ( ) , flags , 0 , lenlow , lenhigh , & mut ov ) } else { UnlockFileEx ( f . as_raw_handle ( ) , 0 , lenlow , lenhigh , & mut ov ) } ; if rc == 0 { let e = Error :: last_os_error ( ) ; if e . raw_os_error ( ) == Some ( ERROR_LOCK_VIOLATION as i32 ) { Err ( ErrorKind :: WouldBlock . into ( ) ) } else { Err ( e ) } } else { Ok ( ( ) ) } }
Acquires and releases a file lock.
Acquires and releases a file lock .
f
&File
lock
Option<Lock>
off
usize
len
usize
wait
bool
f
&File
lock
Option<Lock>
off
usize
len
usize
wait
bool
safety
When used to unlock, this does not guarantee that an exclusive lock is
already held.
When used to unlock this does not guarantee that an exclusive lock is already held .