Struct file_lock::Lock [] [src]

pub struct Lock {
    // some fields omitted
}

Represents a write lock on a file.

The lock(LockKind) method tries to obtain a write-lock on the file identified by a file-descriptor. One can obtain different kinds of write-locks.

Example

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(_) => {
            // we have a lock, which is discarded automatically. Otherwise you could call
            // `unlock()` to make it explicit
            println!("Got lock");
        },
        Err(Error::Errno(i)) => println!("Got filesystem error {}", i),
    }
}

Methods

impl Lock

fn new(fd: RawFd) -> Lock

Create a new lock instance from the given file descriptor fd.

You will have to call lock(...) on it to acquire any lock.

fn lock(&self, kind: LockKind, mode: AccessMode) -> Result<(), Error>

Obtain a write-lock the file-descriptor

For an example, please see the documentation of the Lock structure.

fn unlock(&self) -> Result<(), Error>

Unlocks the file held by Lock.

In reality, you shouldn't need to call unlock(). As Lock implements the Drop trait, once the Lock reference goes out of scope, unlock() will be called automatically.

For an example, please see the documentation of the Lock structure.

Trait Implementations

impl Drop for Lock

fn drop(&mut self)

Derived Implementations

impl PartialEq for Lock

fn eq(&self, __arg_0: &Lock) -> bool

fn ne(&self, __arg_0: &Lock) -> bool

impl Eq for Lock

impl Debug for Lock

fn fmt(&self, __arg_0: &mut Formatter) -> Result