summaryrefslogtreecommitdiff
path: root/crates/vcs_data/src/data/vault
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-10-27 17:59:19 +0800
committerGitHub <noreply@github.com>2025-10-27 17:59:19 +0800
commit5e150adf0e3d8b3843779eddd83469d1b1ba84bc (patch)
treeaedbd6cd10757da9c9d401a818ed1471f377d54b /crates/vcs_data/src/data/vault
parent49ad7a152cf849c8d91ee6b686da31f9c252f77c (diff)
parent368687c943a13427b5338a30fb7b55558420f4de (diff)
Merge pull request #26 from JustEnoughVCS/jvcs_dev
Jvcs dev
Diffstat (limited to 'crates/vcs_data/src/data/vault')
-rw-r--r--crates/vcs_data/src/data/vault/service.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/crates/vcs_data/src/data/vault/service.rs b/crates/vcs_data/src/data/vault/service.rs
new file mode 100644
index 0000000..9fdce85
--- /dev/null
+++ b/crates/vcs_data/src/data/vault/service.rs
@@ -0,0 +1,43 @@
+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 already locked at {}. \
+ To unlock, please stop any running services. \
+ If you are certain no services are running, \
+ please delete this file",
+ 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()) {
+ if e.kind() != std::io::ErrorKind::NotFound {
+ return Err(e);
+ }
+ }
+ Ok(())
+ }
+}