Crate file_lock [] [src]

File locking via POSIX advisory record locks.

This crate provides the facility to obtain a write-lock and unlock a file following the advisory record lock scheme as specified by UNIX IEEE Std 1003.1-2001 (POSIX.1) via fcntl().


Please note that the examples use tempfile merely to quickly create a file which is removed automatically. In the common case, you would want to lock a file which is known to multiple processes.

extern crate file_lock;
extern crate tempfile;

use file_lock::*;
use std::os::unix::io::AsRawFd;

fn main() {
    let f = tempfile::TempFile::new().unwrap();

    match Lock::new(f.as_raw_fd()).lock(LockKind::NonBlocking, AccessMode::Write) {
        Ok(_)                => println!("Got lock"),
        Err(Error::Errno(i)) => println!("Got filesystem error {}", i),



Represents a write lock on a file.



Represents a file access mode, e.g. read or write


Represents the error that occurred while trying to lock or unlock a file.


Represents the kind of lock (e.g. blocking, non-blocking)