Git Product home page Git Product logo

cluflock's Introduction

cluFlock

CI Build Status Platform Apache licensed crates.io 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 2022 #UlinProject Denis Kotlyarov (Денис Котляров)

Licensed under the Apache License, Version 2.0

cluflock's People

Contributors

denisandroid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cluflock's Issues

Make compatible with anyhow

I'm using the anyhow crate for error handling, and when I declare a function to use the anyhow::Result type, I can't get with_context and ? to work properly with functions from this crate here. For example:

SharedFlock::wait_lock(&lock_file)?

generates an error:

error[E0277]: the trait bound `FlockError<&std::fs::File>: StdError` is not satisfied
   --> src\config_file.rs:65:47
    |
65  |             SharedFlock::wait_lock(&lock_file)?
    |                                               ^ the trait `StdError` is not implemented for `FlockError<&std::fs::File>`
    |
    = note: required because of the requirements on the impl of `From<FlockError<&std::fs::File>>` for `anyhow::Error`
    = note: required because of the requirements on the impl of `FromResidual<Result<Infallible, FlockError<&std::fs::File>>>` for `Result<JuliaupConfig, anyhow::Error>`
note: required by `from_residual`
   --> C:\Users\david\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\ops\try_trait.rs:339:5
    |
339 |     fn from_residual(residual: R) -> Self;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0277`.

Not entirely clear to me what the problem is, maybe the error struct in cluFlock just needs to formally implement the std::Error trait? https://crates.io/crates/anyhow might have some info that someone with more Rust experience than me might be able to understand :)

Great package, thanks!

Range-based locks?

Any plans on extending the API to support range-based locks? Both fcntl & LockFileEx support this I think so all platforms should be doable.

`FlockElement` and `WaitFlockUnlock` not implemented for `File` for `wasm32-unknown-unknown` build target

When using cluFlock on a wasm32-unknown-unknown build target I receive the errors

error[E0277]: the trait bound `File: FlockElement` is not satisfied.
error[E0277]: the trait bound `File: WaitFlockUnlock` is not satisfied. 

Context

I am using the settings_manager crate in a Tauri application. settings_manager depends on cluFlock. When building the application, which depends on settings_manager, to a wasm32-unknown-unknown target I receive the errors. However, when building to other targets (specifically x86_64-unknown-linux-gnu) everything builds fine.

Dependency graph
cluFlock <- settings_manager <- Tauri application

I wasn't sure what the most useful info to provide here was, so tried to give an overview. If there is anything else that would be useful, please let me know.

Compatibility with async_std?

If I have an async_std::fs::File object, how do I use this API to acquire a lock? It seems like it operates on the actual std::fs::File object rather than on AsRawFd/AsRawHandle traits?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.