forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock.rs
70 lines (60 loc) · 2.34 KB
/
lock.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process;
use sysinfo::{PidExt, System, SystemExt};
/* This locking mechanism is meant to never be deleted. Instead, it stores the PID of the process
* that's running, when trying to aquire a lock, it checks wether that process is still running. If
* not, it rewrites the lockfile to have its own PID instead. */
pub static LOCKFILE: &str = "rewatch.lock";
pub enum Error {
Locked(u32),
ParsingLockfile(std::num::ParseIntError),
ReadingLockfile(std::io::Error),
WritingLockfile(std::io::Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let msg = match self {
Error::Locked(pid) => format!("Rewatch is already running with PID {}", pid),
Error::ParsingLockfile(e) => format!("Could not parse lockfile: \n {}", e),
Error::ReadingLockfile(e) => format!("Could not read lockfile: \n {}", e),
Error::WritingLockfile(e) => format!("Could not write lockfile: \n {}", e),
};
write!(f, "{}", msg)
}
}
pub enum Lock {
Aquired(u32),
Error(Error),
}
fn exists(to_check_pid: u32) -> bool {
System::new_all()
.processes()
.iter()
.any(|(pid, _process)| pid.as_u32() == to_check_pid)
}
fn create(lockfile_location: &Path, pid: u32) -> Lock {
// Create /lib if not exists
if let Some(Err(e)) = lockfile_location.parent().map(fs::create_dir_all) {
return Lock::Error(Error::WritingLockfile(e));
};
File::create(lockfile_location)
.and_then(|mut file| file.write(pid.to_string().as_bytes()).map(|_| Lock::Aquired(pid)))
.unwrap_or_else(|e| Lock::Error(Error::WritingLockfile(e)))
}
pub fn get(folder: &str) -> Lock {
let location = format!("{}/lib/{}", folder, LOCKFILE);
let path = Path::new(&location);
let pid = process::id();
match fs::read_to_string(&location) {
Err(e) if (e.kind() == std::io::ErrorKind::NotFound) => create(path, pid),
Err(e) => Lock::Error(Error::ReadingLockfile(e)),
Ok(s) => match s.parse::<u32>() {
Ok(parsed_pid) if !exists(parsed_pid) => create(path, pid),
Ok(parsed_pid) => Lock::Error(Error::Locked(parsed_pid)),
Err(e) => Lock::Error(Error::ParsingLockfile(e)),
},
}
}