-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6f3289
commit d14b40b
Showing
6 changed files
with
110 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::fs::{self, File}; | ||
use std::io::{self, Write}; | ||
use std::path::Path; | ||
use std::time::Duration; | ||
|
||
#[napi] | ||
#[derive(Clone)] | ||
pub struct FileLock { | ||
#[napi] | ||
pub locked: bool, | ||
|
||
lock_file_path: String, | ||
} | ||
|
||
#[napi] | ||
impl FileLock { | ||
#[napi(constructor)] | ||
pub fn new(lock_file_path: String) -> anyhow::Result<Self> { | ||
let locked = Path::new(&lock_file_path).exists(); | ||
Ok(Self { | ||
locked, | ||
lock_file_path, | ||
}) | ||
} | ||
|
||
#[napi] | ||
pub fn lock(&mut self) -> anyhow::Result<()> { | ||
if self.locked { | ||
anyhow::bail!("File {} is already locked", self.lock_file_path) | ||
} | ||
let mut lock_file = File::create(&self.lock_file_path)?; | ||
lock_file.write_all(b"")?; | ||
self.locked = true; | ||
Ok(()) | ||
} | ||
|
||
#[napi] | ||
pub fn unlock(&mut self) -> anyhow::Result<()> { | ||
if !self.locked { | ||
anyhow::bail!("File {} is not locked", self.lock_file_path) | ||
} | ||
fs::remove_file(&self.lock_file_path).or_else(|err| { | ||
if err.kind() == io::ErrorKind::NotFound { | ||
Ok(()) | ||
} else { | ||
Err(err) | ||
} | ||
})?; | ||
self.locked = false; | ||
Ok(()) | ||
} | ||
|
||
#[napi] | ||
pub async fn wait(&self, timeout: Option<i32>) -> Result<(), napi::Error> { | ||
if !self.locked { | ||
return Ok(()); | ||
} | ||
|
||
let start = std::time::Instant::now(); | ||
let duration = timeout.map(|t| Duration::from_secs(u64::try_from(t).unwrap())); | ||
|
||
loop { | ||
if !self.locked || !Path::new(&self.lock_file_path).exists() { | ||
break Ok(()); | ||
} | ||
|
||
if let Some(duration) = duration { | ||
if start.elapsed() > duration { | ||
break Err(napi::Error::from_reason("Timeout waiting for lock")); | ||
} | ||
} | ||
|
||
std::thread::sleep(Duration::from_millis(2)); | ||
} | ||
} | ||
} | ||
|
||
// Ensure the lock file is removed when the FileLock is dropped | ||
impl Drop for FileLock { | ||
fn drop(&mut self) { | ||
if self.locked { | ||
let _ = self.unlock(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.