#file-lock #byte-range #locking #file-access #lock

file-guard

A cross-platform library for simple advisory file locking

2 unstable releases

0.2.0 Mar 9, 2024
0.1.0 Dec 26, 2021

#200 in Filesystem

Download history 34591/week @ 2024-08-20 47133/week @ 2024-08-27 50953/week @ 2024-09-03 46999/week @ 2024-09-10 47052/week @ 2024-09-17 47835/week @ 2024-09-24 51884/week @ 2024-10-01 48332/week @ 2024-10-08 51606/week @ 2024-10-15 55503/week @ 2024-10-22 51115/week @ 2024-10-29 60731/week @ 2024-11-05 49606/week @ 2024-11-12 52059/week @ 2024-11-19 50558/week @ 2024-11-26 37722/week @ 2024-12-03

200,342 downloads per month
Used in 574 crates (3 directly)

MIT license

24KB
308 lines

Verify

file-guard

A cross-platform library for simple advisory file locking in Rust.

Take a look at the Documentation for details!

The lock supports both exclusive and shared locking modes for a byte range of an opened File object. Exclusively locking a portion of a file denies all other processes both shared and exclusive access to the specified region of the file. Shared locking a portion of a file denies all processes exclusive access to the specified region of the file. The locked range does not need to exist within the file, and the ranges may be used for any arbitrary advisory locking protocol between processes.

The result of a lock(), try_lock(), or lock_any() is a FileGuard. When dropped, this FileGuard will unlock the region of the file currently held. Exclusive locks may be .downgrade()'ed to either a shared lock cross platform.

On Unix systems fcntl is used to perform the locking, and on Windows, LockFileEx. All generally available behavior is consistent across platforms. For platform- specific behavior, traits may be used for the respective platform. For example, on Windows, locks cannot be safely upgraded, whereas on Unix systems, this can be done safely and atomically. To use this feature, the file_guard::os::unix::FileGuardExt may useed, enabling the .upgrade() and .try_upgrade() methods.

Note that on Windows, the file must be open with write permissions to lock it.

Examples

use file_guard::Lock;
use std::fs::OpenOptions;

let mut file = OpenOptions::new()
    .read(true)
    .write(true)
    .create(true)
    .open("example-lock")?;

let mut lock = file_guard::lock(&mut file, Lock::Exclusive, 0, 1)?;
write_to_file(&mut lock)?;
// the lock will be unlocked when it goes out of scope

You can store one or more locks in a struct:

use file_guard::{FileGuard, Lock};
use std::fs::{File, OpenOptions};

let file = OpenOptions::new()
    .read(true)
    .write(true)
    .create(true)
    .open("example-lock")?;

struct Thing<'file> {
    a: FileGuard<&'file File>,
    b: FileGuard<&'file File>,
}

let t = Thing {
    a: file_guard::lock(&file, Lock::Exclusive, 0, 1)?,
    b: file_guard::lock(&file, Lock::Shared, 1, 2)?,
};
// both locks will be unlocked when t goes out of scope

Anything that can Deref or DerefMut to a File can be used with the FileGuard. This works with Rc<File>:

use file_guard::{FileGuard, Lock};
use std::fs::{File, OpenOptions};
use std::rc::Rc;

let file = Rc::new(
    OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open("example-lock")?
);

struct Thing {
    a: FileGuard<Rc<File>>,
    b: FileGuard<Rc<File>>,
}

let t = Thing {
    a: file_guard::lock(file.clone(), Lock::Exclusive, 0, 1)?,
    b: file_guard::lock(file, Lock::Shared, 1, 2)?,
};
// both locks will be unlocked and the file will be closed when t goes out of scope

Dependencies

~215KB