Struct file_lock::Lock [] [src]

pub struct Lock { /* 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.

  • LockKind::NonBlocking - immediately return with an Errno error.
  • LockKind::Blocking - waits (i.e. blocks the running thread) for the current owner of the lock to relinquish the lock.

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
[src]

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

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

Obtain a write-lock the file-descriptor

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

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 Debug for Lock
[src]

Formats the value using the given formatter.

impl Eq for Lock
[src]

impl PartialEq for Lock
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Drop for Lock
[src]

A method called when the value goes out of scope. Read more