Skip to content

Commit

Permalink
1. Preparation for publication.
Browse files Browse the repository at this point in the history
  • Loading branch information
denisandroid committed Apr 7, 2019
1 parent 10c5714 commit 3d0d029
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 160 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cluFlock"
version = "0.2.1"
version = "0.2.2"
authors = ["Denis Kotlyarov (Денис Котляров) <[email protected]>"]
repository = "https://github.com/clucompany/cluFlock.git"
license = " Apache-2.0"
Expand All @@ -10,7 +10,7 @@ edition = "2018"
keywords = ["flock", "unix_flock", "flock_lock_methods", "clucompany"]
categories = ["development-tools"]

description = "Control of lock of the file using the 'flock' functions."
description = "Establishes and safely deletes advisory blocking on the open file."

[dependencies]
libc = "0.2.51"
178 changes: 87 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,129 +5,125 @@
[![crates.io](http://meritbadge.herokuapp.com/cluFlock)](https://crates.io/crates/cluFlock)
[![Documentation](https://docs.rs/cluFlock/badge.svg)](https://docs.rs/cluFlock)

Control of lock of the file using the 'flock' functions.


# Capabilities

1. Convenient and transparent trait of a call of locks.
2. Automatic unlocking of lock.

# Locks

1. ExclusiveFlock - To establish exclusive blocking. Only one process can hold exclusive blocking of the file..
2. SharedFlock - Set a shared lock. A shared lock on a given file can hold more than one process.

Establishes and safely deletes advisory blocking on the open file.

# Use
1. Exclusive LockFile

```
extern crate cluFlock;
extern crate cluFlock;
use cluFlock::ToFlock;
use std::fs::File;
use std::io;
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()?;
fn main() -> Result<(), io::Error> {
let file_lock = File::create("/tmp/1")?.wait_exclusive_lock()?;

println!("{:?}", file_lock);
drop(file_lock); //<-- unlock fn.
println!("{:?}", file_lock);
drop(file_lock); //<-- unlock fn.
Ok( () )
}
Ok( () )
}
```

2. Exclusive LockFile (FnOnce)

```
extern crate cluFlock;
2. Exclusive LockClosure
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.")
})??;
extern crate cluFlock;
Ok( () )
}
```

use std::io::Write;
use cluFlock::ToFlock;
use std::fs::File;
use std::io;
3. Exclusive LockFile (&File)

fn main() -> Result<(), io::Error> {
File::create("/tmp/1")?.wait_exclusive_lock_fn(|mut file| {
write!(file, "Test.")
})??;
```
Ok( () )
}
extern crate cluFlock;
3. The temporary file for interprogram synchronization
use cluFlock::ExclusiveFlock;
use std::fs::File;
fn main() -> Result<(), std::io::Error> {
let file = File::create("/tmp/1").unwrap();
extern crate cluFlock;
let file_lock = ExclusiveFlock::wait_lock(&file)?;
//lock...
use cluFlock::ToFlock;
use cluFlock::FileFlock;
use std::io::ErrorKind::AlreadyExists;
use std::path::Path;
use std::fs;
use std::io;
use std::fs::OpenOptions;
println!("{:?}", file_lock);
// let file move!
drop(file_lock);
//Example
//The temporary file for interprogram synchronization.
file.sync_all()?;
Ok( () )
}
```

#[derive(Debug)]
pub struct MyLockFile<'a>(FileFlock, Option<&'a Path>);
4. LockFile (use try_exclusive_lock)

impl<'a> MyLockFile<'a> {
pub fn new(p: &'a Path) -> Result<Self, io::Error> {
let (lock, path) = match OpenOptions::new().write(true).create_new(true).open(p) {
Ok(file) => (file.wait_exclusive_lock()?, Some(p)),
Err(ref e) if e.kind() == AlreadyExists => {
let f = OpenOptions::new().read(true).open(p)?;
```
let lock = f.try_exclusive_lock()?;
(lock, None)
},
Err(e) => return Err(e),
};
extern crate cluFlock;
Ok( MyLockFile(lock, path) )
}
}
use cluFlock::ExclusiveFlock;
use std::fs::File;
use std::time::Duration;
use std::io::ErrorKind;
impl<'a> Drop for MyLockFile<'a> {
fn drop(&mut self) {
//Not obligatory action.
//
fn main() {
let file: File = match File::create("/tmp/ulin.lock") {
Ok(a) => a,
Err(e) => panic!("Panic, err create file {:?}", e),
};
//Not to delete the file if it initially existed.
println!("Try_Exclusive_Lock, {:?}", file);
if let Some(path) = self.1 {
let _e = fs::remove_file(path);
}
}
}


pub fn main() -> Result<(), io::Error> {
let path = Path::new("/tmp/flock.lock");
println!("LockFile {:?}", path);
let lock_file = MyLockFile::new(path)?;

println!("OK! FileFlock {:?}", lock_file);
for a in 0..4 {
println!("Sleep {}", a);
::std::thread::sleep(::std::time::Duration::from_secs(1));
}

drop(lock_file);

Ok( () )
}
let lock = match ExclusiveFlock::try_lock(&file) {
//Success, we blocked the file.
Ok(lock) => {
println!("OK, File {:?} successfully locked.", file);
lock
},
//File already locked.
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {
println!("ALREADY LOCKED: File {:?}.", file);
println!("!Exclusive_Lock, {:?}", file);
//Lock the current thread to such an extent until your file is unlocked.
//&file.wait_exclusive_lock().unwrap()
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);
}
```



Expand Down
Loading

0 comments on commit 3d0d029

Please sign in to comment.