try_lock_with_pid/
try_lock_with_pid.rs1use fslock_arti_fork as fslock;
2
3#[cfg(feature = "std")]
4use fslock::LockFile;
5#[cfg(feature = "std")]
6use std::{env, fs::read_to_string, process};
7
8#[cfg(feature = "std")]
9fn main() -> Result<(), fslock::Error> {
10 let mut args = env::args();
11 args.next();
12
13 let path = match args.next() {
14 Some(arg) if args.next().is_none() => arg,
15 _ => {
16 eprintln!("Expected one argument");
17 process::exit(1);
18 },
19 };
20
21 let mut lockfile = LockFile::open(&path)?;
22
23 if lockfile.try_lock_with_pid()? {
24 let content_a = read_to_string(&path)?;
25 let content_b = read_to_string(&path)?;
26 assert!(content_a.trim().len() > 0);
27 assert!(content_a.trim().chars().all(|ch| ch.is_ascii_digit()));
28 assert_eq!(content_a, content_b);
29
30 println!("{}", content_a);
31 } else {
32 println!("FAILURE");
33 }
34
35 Ok(())
36}
37
38#[cfg(not(feature = "std"))]
39fn main() {}