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
libcandio::Resulttypes from the standard library. - If the file lock operation fails, an
io::Erroris returned.
Modules§
- pid
- Pid
Traits§
- FdLock
- The
FdLocktrait extends theAsRawFdtrait, allowing file locks to be placed on file descriptors.
Type Aliases§
- Operation
- FdLock Operation type.