Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/clucompany/cluflock
Installation and subsequent safe removal of flock locks for data streams.
https://github.com/clucompany/cluflock
clucompany file-lock flock rust rust-library safe-flock stream-flock unix-lock win-lock
Last synced: about 1 month ago
JSON representation
Installation and subsequent safe removal of flock locks for data streams.
- Host: GitHub
- URL: https://github.com/clucompany/cluflock
- Owner: clucompany
- License: apache-2.0
- Created: 2018-11-10T20:11:32.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-19T13:02:04.000Z (8 months ago)
- Last Synced: 2024-11-08T01:54:27.770Z (about 2 months ago)
- Topics: clucompany, file-lock, flock, rust, rust-library, safe-flock, stream-flock, unix-lock, win-lock
- Language: Rust
- Homepage:
- Size: 158 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cluFlock
[![CI](https://github.com/clucompany/cluFlock/actions/workflows/CI.yml/badge.svg?event=push)](https://github.com/clucompany/cluFlock/actions/workflows/CI.yml)
[![Build Status](https://travis-ci.org/clucompany/cluFlock.svg?branch=master)](https://travis-ci.org/clucompany/cluFlock)
[![Platform](https://img.shields.io/badge/platform-unix%20|%20linux%20|%20windows-blue)](https://github.com/clucompany/cluFlock/tree/master/tests)
[![Apache licensed](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](./LICENSE)
[![crates.io](https://img.shields.io/crates/v/cluFlock)](https://crates.io/crates/cluFlock)
[![Documentation](https://docs.rs/cluFlock/badge.svg)](https://docs.rs/cluFlock)Installation and subsequent safe removal of `flock` locks for data streams.
# Use
1. Exclusive LockFile```rust
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( () )
}
```2. Exclusive LockFile (FnOnce)
```rust
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(())
}
```3. Exclusive LockFile (&File)
```rust
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 ExclusiveFlockfile.sync_all()?;
Ok( () )
}
```4. Shared LockFile (&File)
```rust
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).
1. 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