summaryrefslogtreecommitdiff
path: root/legacy_data/src/data/vault/lock_status.rs
diff options
context:
space:
mode:
Diffstat (limited to 'legacy_data/src/data/vault/lock_status.rs')
-rw-r--r--legacy_data/src/data/vault/lock_status.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/legacy_data/src/data/vault/lock_status.rs b/legacy_data/src/data/vault/lock_status.rs
new file mode 100644
index 0000000..3f59c30
--- /dev/null
+++ b/legacy_data/src/data/vault/lock_status.rs
@@ -0,0 +1,40 @@
+use std::path::PathBuf;
+
+use crate::{constants::SERVER_FILE_LOCKFILE, data::vault::Vault};
+
+impl Vault {
+ /// Get the path of the lock file for the current Vault
+ pub fn lock_file_path(&self) -> PathBuf {
+ self.vault_path().join(SERVER_FILE_LOCKFILE)
+ }
+
+ /// Check if the current Vault is locked
+ pub fn is_locked(&self) -> bool {
+ self.lock_file_path().exists()
+ }
+
+ /// Lock the current Vault
+ pub fn lock(&self) -> Result<(), std::io::Error> {
+ if self.is_locked() {
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::AlreadyExists,
+ format!(
+ "Vault is locked! This indicates a service is already running here.\nPlease stop other services or delete the lock file at the vault root directory: {}",
+ self.lock_file_path().display()
+ ),
+ ));
+ }
+ std::fs::File::create(self.lock_file_path())?;
+ Ok(())
+ }
+
+ /// Unlock the current Vault
+ pub fn unlock(&self) -> Result<(), std::io::Error> {
+ if let Err(e) = std::fs::remove_file(self.lock_file_path())
+ && e.kind() != std::io::ErrorKind::NotFound
+ {
+ return Err(e);
+ }
+ Ok(())
+ }
+}