From eb167d3792e6af987425508dc806595f6be1f79c Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 29 Oct 2025 15:24:46 +0800 Subject: Make config fields thread-safe with Arc - Change LocalWorkspace config to Arc> - Change Vault config to Arc - Add config accessor methods for both structs - Update initialization methods to wrap config in Arc/Mutex --- crates/vcs_data/src/data/local.rs | 21 ++++++++++++++++----- crates/vcs_data/src/data/vault.rs | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 8 deletions(-) (limited to 'crates/vcs_data/src') diff --git a/crates/vcs_data/src/data/local.rs b/crates/vcs_data/src/data/local.rs index c93bd2b..fb43042 100644 --- a/crates/vcs_data/src/data/local.rs +++ b/crates/vcs_data/src/data/local.rs @@ -1,7 +1,7 @@ -use std::{env::current_dir, path::PathBuf}; +use std::{env::current_dir, path::PathBuf, sync::Arc}; use cfg_file::config::ConfigFile; -use tokio::fs; +use tokio::{fs, sync::Mutex}; use crate::{ constants::{CLIENT_FILE_README, CLIENT_FILE_WORKSPACE}, @@ -12,7 +12,7 @@ use crate::{ pub mod config; pub struct LocalWorkspace { - config: LocalConfig, + config: Arc>, local_path: PathBuf, } @@ -25,13 +25,19 @@ impl LocalWorkspace { /// Initialize local workspace. pub fn init(config: LocalConfig, local_path: impl Into) -> Option { let local_path = find_local_path(local_path)?; - Some(Self { config, local_path }) + Some(Self { + config: Arc::new(Mutex::new(config)), + local_path, + }) } /// Initialize local workspace in the current directory. pub fn init_current_dir(config: LocalConfig) -> Option { let local_path = current_local_path()?; - Some(Self { config, local_path }) + Some(Self { + config: Arc::new(Mutex::new(config)), + local_path, + }) } /// Setup local workspace @@ -92,6 +98,11 @@ Without these credentials, the server will reject all access requests. Ok(()) } + /// Get a reference to the local configuration. + pub fn config(&self) -> Arc> { + self.config.clone() + } + /// Setup local workspace in current directory pub async fn setup_local_workspace_current_dir() -> Result<(), std::io::Error> { Self::setup_local_workspace(current_dir()?).await?; diff --git a/crates/vcs_data/src/data/vault.rs b/crates/vcs_data/src/data/vault.rs index 80ebe1d..efb4eec 100644 --- a/crates/vcs_data/src/data/vault.rs +++ b/crates/vcs_data/src/data/vault.rs @@ -2,6 +2,7 @@ use std::{ env::current_dir, fs::{self, create_dir_all}, path::PathBuf, + sync::Arc, }; use cfg_file::config::ConfigFile; @@ -22,7 +23,7 @@ pub mod sheets; pub mod virtual_file; pub struct Vault { - config: VaultConfig, + config: Arc, vault_path: PathBuf, } @@ -35,13 +36,19 @@ impl Vault { /// Initialize vault pub fn init(config: VaultConfig, vault_path: impl Into) -> Option { let vault_path = find_vault_path(vault_path)?; - Some(Self { config, vault_path }) + Some(Self { + config: Arc::new(config), + vault_path, + }) } /// Initialize vault pub fn init_current_dir(config: VaultConfig) -> Option { let vault_path = current_vault_path()?; - Some(Self { config, vault_path }) + Some(Self { + config: Arc::new(config), + vault_path, + }) } /// Setup vault @@ -144,4 +151,9 @@ Thank you for using `JustEnoughVCS!` Self::setup_vault(current_dir()?).await?; Ok(()) } + + /// Get vault configuration + pub fn config(&self) -> &Arc { + &self.config + } } -- cgit From 05de2da565a32ab3b144f7b95860897a13c895f7 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 29 Oct 2025 15:25:05 +0800 Subject: Improve vault lock error message and formatting - Use clearer error message when vault is already locked - Fix code formatting for consistency - Remove unnecessary line breaks in error formatting --- crates/vcs_data/src/data/vault/service.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'crates/vcs_data/src') diff --git a/crates/vcs_data/src/data/vault/service.rs b/crates/vcs_data/src/data/vault/service.rs index 22e91d5..3f59c30 100644 --- a/crates/vcs_data/src/data/vault/service.rs +++ b/crates/vcs_data/src/data/vault/service.rs @@ -9,7 +9,7 @@ impl Vault { } /// Check if the current Vault is locked - pub fn is_locked(&self) -> bool { + pub fn is_locked(&self) -> bool { self.lock_file_path().exists() } @@ -19,10 +19,7 @@ impl Vault { 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", + "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() ), )); @@ -34,9 +31,10 @@ impl Vault { /// 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); - } + && e.kind() != std::io::ErrorKind::NotFound + { + return Err(e); + } Ok(()) } } -- cgit