cluFlock

Installation and subsequent safe removal of flock locks for data streams.
Use
- Exclusive LockFile
use cluFlock::ToFlock;
use std::fs::File;
use std::io;
fn main() -> Result<(), io::Error> {
let file_lock = File::create("/tmp/1")?.wait_exclusive_lock()?;
println!("{:?}", file_lock);
drop(file_lock);
Ok( () )
}
- Exclusive LockFile (FnOnce)
use std::io::Write;
use cluFlock::ToFlock;
use std::fs::File;
use std::io;
fn main() -> Result<(), io::Error> {
File::create("/tmp/1")?.wait_exclusive_lock_fn(|mut file| {
write!(file, "Test.")
})??;
Ok( () )
}
- Exclusive LockFile (&File)
extern crate cluFlock;
use cluFlock::ExclusiveFlock;
use std::fs::File;
fn main() -> Result<(), std::io::Error> {
let file = File::create("/tmp/1").unwrap();
let file_lock = ExclusiveFlock::wait_lock(&file)?;
println!("{:?}", file_lock);
drop(file_lock);
file.sync_all()?;
Ok( () )
}
- LockFile (use try_exclusive_lock)
use cluFlock::ExclusiveFlock;
use std::fs::File;
use std::time::Duration;
use std::io::ErrorKind;
fn main() {
let file: File = match File::create("/tmp/ulin.lock") {
Ok(a) => a,
Err(e) => panic!("Panic, err create file {:?}", e),
};
println!("Try_Exclusive_Lock, {:?}", file);
let lock = match ExclusiveFlock::try_lock(&file) {
Ok(lock) => {
println!("OK, File {:?} successfully locked.", file);
lock
},
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {
println!("ALREADY LOCKED: File {:?}.", file);
println!("!Exclusive_Lock, {:?}", file);
ExclusiveFlock::wait_lock(&file).unwrap()
},
Err(e) => panic!("Panic, err lock file {:?}", e)
};
println!("Sleep, 5s");
::std::thread::sleep(Duration::from_secs(5));
println!("Unlock, {:?}", file);
drop(lock);
}
Library flags:
- nightly: Allows you to safely transform the lock into the original data, the night version of the compiler and the cluFullTransmute library are required.
License
Copyright 2019 #UlinProject Denis Kotlyarov (Денис Котляров)
Licensed under the Apache License, Version 2.0