Crate filelock_rs
source ·Expand description
FdLock
FdLock
is a Rust crate that provides functionality for file locking using flock
or fcntl
operations.
This crate defines a trait FdLock
that extends the AsRawFd
trait,
allowing file locks to be placed on file descriptors. It supports both
shared and exclusive locks, as well as unlocking operations.
Examples
Placing a shared lock on a file:
use filelock_rs::FdLock;
use std::fs::File;
let file = File::open("data.txt").expect("Failed to open file");
let lock_result = file.lock_shared();
match lock_result {
Ok(()) => {
println!("Shared lock placed on the file");
// Perform operations with the locked file
}
Err(error) => {
eprintln!("Failed to place a shared lock: {}", error);
}
}
Placing an exclusive lock on a file:
use filelock_rs::FdLock;
use std::fs::File;
let file = File::create("data.txt").expect("Failed to create file");
let lock_result = file.lock_exclusive();
match lock_result {
Ok(()) => {
println!("Exclusive lock placed on the file");
// Perform operations with the locked file
}
Err(error) => {
eprintln!("Failed to place an exclusive lock: {}", error);
}
}
Cleanup
The FdLock
trait is implemented for the std::fs::File
type. When the FdLock
methods are
used on a File
instance, the locks are automatically released when they go out of scope.
Notes
- The behavior of file locking may differ depending on the operating system.
- The crate uses the
libc
andio::Result
types from the standard library. - If the file lock operation fails, an
io::Error
is returned.
Modules
- Pid
Traits
- The
FdLock
trait extends theAsRawFd
trait, allowing file locks to be placed on file descriptors.
Type Definitions
- FdLock Operation type.