cluFlock

Installation and subsequent safe removal of flock locks for data streams

7 releases (3 stable)

1.2.7 Feb 15, 2022
1.2.5 Sep 18, 2019
1.2.4 Aug 31, 2019
0.2.2 Apr 7, 2019
0.1.5 Nov 12, 2018

#64 in FFI

Download history 190/week @ 2024-06-13 260/week @ 2024-06-20 163/week @ 2024-06-27 159/week @ 2024-07-04 496/week @ 2024-07-11 191/week @ 2024-07-18 253/week @ 2024-07-25 176/week @ 2024-08-01 385/week @ 2024-08-08 278/week @ 2024-08-15 191/week @ 2024-08-22 351/week @ 2024-08-29 332/week @ 2024-09-05 250/week @ 2024-09-12 240/week @ 2024-09-19 164/week @ 2024-09-26

1,078 downloads per month
Used in 17 crates (8 directly)

Apache-2.0

37KB
839 lines

cluFlock

Build Status Platform Apache licensed Documentation

Installation and subsequent safe removal of flock locks for data streams.

Use

  1. Exclusive LockFile
use cluFlock::ToFlock;
use std::fs::File;
use std::io;

fn main() -> Result<(), io::Error> {
	let file_lock = File::create("./file")?.wait_exclusive_lock()?;
	println!("{:?}", file_lock);
	
	Ok( () )
}
  1. Exclusive LockFile (FnOnce)
use std::io::Write;
use cluFlock::ToFlock;
use std::fs::File;
use std::io;

fn main() -> Result<(), io::Error> {
	File::create("./file")?.wait_exclusive_lock_fn(
		// valid exclusive lock
		|mut file| write!(file, "Test."), // result: Ok(usize)/Err(std::io::Error)
		
		// invalid lock
		|err| Err(err.into_err()) // into_err: FlockErr -> std::io::Error
	)?;
	
	Ok(())
}
  1. Exclusive LockFile (&File)
use cluFlock::ExclusiveFlock;
use std::fs::File;

fn main() -> Result<(), std::io::Error> {
	let file = File::create("./file")?;
	
	{
		let file_lock = ExclusiveFlock::wait_lock(&file)?;
		// file_lock, type: FlockLock<&File>

		println!("{:?}", file_lock);
	} // auto unlock ExclusiveFlock

	file.sync_all()?;

	Ok( () )
}
  1. Shared LockFile (&File)
use std::fs::File;
use cluFlock::SharedFlock;
use std::io;

fn main() -> Result<(), io::Error> {
	let file = File::create("./test_file")?;
	
	let shared = SharedFlock::wait_lock(&file);
	println!("#1shared {:?}", shared);
	let shared2 = SharedFlock::try_lock(&file);
	println!("#2shared {:?}", shared2);
	
	assert_eq!(shared.is_ok(), true);
	assert_eq!(shared2.is_ok(), true);
	
	// manual or automatic unlock SharedFlock_x2
	// drop(shared);
	// drop(shared2);
	
	Ok( () )
}

Support of platforms:

  1. Unix, Linux: Full support: SharedFlock (Wait, Try), ExclusiveFlock (Wait, Try), Unlock (Wait, Try).
  2. Windows: Full support: SharedFlock (Wait, Try), ExclusiveFlock (Wait, Try), Unlock (Wait, !Try). Unlock Try is not implemented and is considered additional unsafe functionality.

Features of platforms:

  1. Unix, Linux: The flock system call only works between processes, there are no locks inside the process.
  2. Windows: System calls (LockFileEx UnlockFileEx) work between processes and within the current process. If you use Shared and Exclusive locks, you can lock yourself in the same process.

License

Copyright 2021 #UlinProject Denis Kotlyarov (Денис Котляров)

Licensed under the Apache License, Version 2.0

Dependencies

~220KB