From 27f6414ad1ff451feb0044af62f37dc2a6255ffa Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 5 Feb 2026 22:35:05 +0800 Subject: Remove examples and legacy code, update .gitignore - Delete examples directory and its example action system - Rename actions/ to legacy_actions/ and data/ to legacy_data/ - Update Cargo.toml license file reference - Move setup scripts to scripts/dev/ directory - Add todo.txt patterns to .gitignore --- .gitignore | 2 + Cargo.lock | 13 - Cargo.toml | 18 +- actions/Cargo.toml | 29 - actions/src/connection.rs | 3 - actions/src/connection/action_service.rs | 221 ----- actions/src/connection/error.rs | 27 - actions/src/connection/protocol.rs | 7 - actions/src/lib.rs | 4 - actions/src/local_actions.rs | 2 - actions/src/local_actions/account_manage.rs | 3 - .../account_manage/register_account.rs | 0 .../local_actions/account_manage/remove_account.rs | 0 .../local_actions/account_manage/switch_account.rs | 0 actions/src/local_actions/current_sheet.rs | 2 - .../src/local_actions/current_sheet/exit_sheet.rs | 0 .../src/local_actions/current_sheet/use_sheet.rs | 0 actions/src/registry.rs | 2 - actions/src/registry/client_registry.rs | 128 --- actions/src/registry/server_registry.rs | 43 - actions/src/remote_actions.rs | 288 ------ actions/src/remote_actions/content_manage.rs | 1 - .../remote_actions/content_manage/track_file.rs | 985 --------------------- actions/src/remote_actions/edit_right_manage.rs | 1 - .../change_virtual_file_edit_right.rs | 144 --- actions/src/remote_actions/mapping_manage.rs | 3 - .../remote_actions/mapping_manage/edit_mapping.rs | 157 ---- .../mapping_manage/merge_share_mapping.rs | 117 --- .../remote_actions/mapping_manage/share_mapping.rs | 135 --- actions/src/remote_actions/sheet_manage.rs | 2 - .../src/remote_actions/sheet_manage/drop_sheet.rs | 123 --- .../src/remote_actions/sheet_manage/make_sheet.rs | 98 -- actions/src/remote_actions/workspace_manage.rs | 2 - .../workspace_manage/set_upstream_vault.rs | 101 --- .../workspace_manage/update_to_latest_info.rs | 433 --------- data/Cargo.toml | 40 - data/src/constants.rs | 140 --- data/src/data.rs | 5 - data/src/data/local.rs | 266 ------ data/src/data/local/align_tasks.rs | 110 --- data/src/data/local/cached_sheet.rs | 94 -- data/src/data/local/latest_file_data.rs | 103 --- data/src/data/local/latest_info.rs | 81 -- data/src/data/local/local_files.rs | 148 ---- data/src/data/local/local_sheet.rs | 439 --------- data/src/data/local/modified_status.rs | 30 - data/src/data/local/workspace_analyzer.rs | 359 -------- data/src/data/local/workspace_config.rs | 374 -------- data/src/data/member.rs | 71 -- data/src/data/sheet.rs | 278 ------ data/src/data/user.rs | 28 - data/src/data/user/accounts.rs | 162 ---- data/src/data/vault.rs | 132 --- data/src/data/vault/lock_status.rs | 40 - data/src/data/vault/mapping_share.rs | 422 --------- data/src/data/vault/member_manage.rs | 144 --- data/src/data/vault/sheet_manage.rs | 274 ------ data/src/data/vault/vault_config.rs | 233 ----- data/src/data/vault/virtual_file.rs | 500 ----------- data/src/env.rs | 84 -- data/src/lib.rs | 5 - data/tests/Cargo.toml | 13 - data/tests/src/lib.rs | 30 - ...local_workspace_setup_and_account_management.rs | 248 ------ ...st_sheet_creation_management_and_persistence.rs | 275 ------ .../test_sheet_share_creation_and_management.rs | 631 ------------- .../src/test_vault_setup_and_member_register.rs | 67 -- .../src/test_virtual_file_creation_and_update.rs | 162 ---- examples/Cargo.toml | 25 - examples/src/bin/example_action_system.rs | 18 - examples/src/lib.rs | 1 - legacy_actions/Cargo.toml | 29 + legacy_actions/src/connection.rs | 3 + legacy_actions/src/connection/action_service.rs | 221 +++++ legacy_actions/src/connection/error.rs | 27 + legacy_actions/src/connection/protocol.rs | 7 + legacy_actions/src/lib.rs | 4 + legacy_actions/src/local_actions.rs | 2 + legacy_actions/src/local_actions/account_manage.rs | 3 + .../account_manage/register_account.rs | 0 .../local_actions/account_manage/remove_account.rs | 0 .../local_actions/account_manage/switch_account.rs | 0 legacy_actions/src/local_actions/current_sheet.rs | 2 + .../src/local_actions/current_sheet/exit_sheet.rs | 0 .../src/local_actions/current_sheet/use_sheet.rs | 0 legacy_actions/src/registry.rs | 2 + legacy_actions/src/registry/client_registry.rs | 128 +++ legacy_actions/src/registry/server_registry.rs | 43 + legacy_actions/src/remote_actions.rs | 288 ++++++ .../src/remote_actions/content_manage.rs | 1 + .../remote_actions/content_manage/track_file.rs | 985 +++++++++++++++++++++ .../src/remote_actions/edit_right_manage.rs | 1 + .../change_virtual_file_edit_right.rs | 144 +++ .../src/remote_actions/mapping_manage.rs | 3 + .../remote_actions/mapping_manage/edit_mapping.rs | 157 ++++ .../mapping_manage/merge_share_mapping.rs | 117 +++ .../remote_actions/mapping_manage/share_mapping.rs | 135 +++ legacy_actions/src/remote_actions/sheet_manage.rs | 2 + .../src/remote_actions/sheet_manage/drop_sheet.rs | 123 +++ .../src/remote_actions/sheet_manage/make_sheet.rs | 98 ++ .../src/remote_actions/workspace_manage.rs | 2 + .../workspace_manage/set_upstream_vault.rs | 101 +++ .../workspace_manage/update_to_latest_info.rs | 433 +++++++++ legacy_data/Cargo.toml | 40 + legacy_data/src/constants.rs | 140 +++ legacy_data/src/data.rs | 5 + legacy_data/src/data/local.rs | 266 ++++++ legacy_data/src/data/local/align_tasks.rs | 110 +++ legacy_data/src/data/local/cached_sheet.rs | 94 ++ legacy_data/src/data/local/latest_file_data.rs | 103 +++ legacy_data/src/data/local/latest_info.rs | 81 ++ legacy_data/src/data/local/local_files.rs | 148 ++++ legacy_data/src/data/local/local_sheet.rs | 439 +++++++++ legacy_data/src/data/local/modified_status.rs | 30 + legacy_data/src/data/local/workspace_analyzer.rs | 359 ++++++++ legacy_data/src/data/local/workspace_config.rs | 374 ++++++++ legacy_data/src/data/member.rs | 71 ++ legacy_data/src/data/sheet.rs | 278 ++++++ legacy_data/src/data/user.rs | 28 + legacy_data/src/data/user/accounts.rs | 162 ++++ legacy_data/src/data/vault.rs | 132 +++ legacy_data/src/data/vault/lock_status.rs | 40 + legacy_data/src/data/vault/mapping_share.rs | 422 +++++++++ legacy_data/src/data/vault/member_manage.rs | 144 +++ legacy_data/src/data/vault/sheet_manage.rs | 274 ++++++ legacy_data/src/data/vault/vault_config.rs | 233 +++++ legacy_data/src/data/vault/virtual_file.rs | 500 +++++++++++ legacy_data/src/env.rs | 84 ++ legacy_data/src/lib.rs | 5 + legacy_data/tests/Cargo.toml | 13 + legacy_data/tests/src/lib.rs | 30 + ...local_workspace_setup_and_account_management.rs | 248 ++++++ ...st_sheet_creation_management_and_persistence.rs | 275 ++++++ .../test_sheet_share_creation_and_management.rs | 631 +++++++++++++ .../src/test_vault_setup_and_member_register.rs | 67 ++ .../src/test_virtual_file_creation_and_update.rs | 162 ++++ scripts/dev/setup.ps1 | 22 + scripts/dev/setup.sh | 12 + setup.ps1 | 17 - setup.sh | 9 - src/lib.rs | 6 +- 141 files changed, 9096 insertions(+), 9145 deletions(-) delete mode 100644 actions/Cargo.toml delete mode 100644 actions/src/connection.rs delete mode 100644 actions/src/connection/action_service.rs delete mode 100644 actions/src/connection/error.rs delete mode 100644 actions/src/connection/protocol.rs delete mode 100644 actions/src/lib.rs delete mode 100644 actions/src/local_actions.rs delete mode 100644 actions/src/local_actions/account_manage.rs delete mode 100644 actions/src/local_actions/account_manage/register_account.rs delete mode 100644 actions/src/local_actions/account_manage/remove_account.rs delete mode 100644 actions/src/local_actions/account_manage/switch_account.rs delete mode 100644 actions/src/local_actions/current_sheet.rs delete mode 100644 actions/src/local_actions/current_sheet/exit_sheet.rs delete mode 100644 actions/src/local_actions/current_sheet/use_sheet.rs delete mode 100644 actions/src/registry.rs delete mode 100644 actions/src/registry/client_registry.rs delete mode 100644 actions/src/registry/server_registry.rs delete mode 100644 actions/src/remote_actions.rs delete mode 100644 actions/src/remote_actions/content_manage.rs delete mode 100644 actions/src/remote_actions/content_manage/track_file.rs delete mode 100644 actions/src/remote_actions/edit_right_manage.rs delete mode 100644 actions/src/remote_actions/edit_right_manage/change_virtual_file_edit_right.rs delete mode 100644 actions/src/remote_actions/mapping_manage.rs delete mode 100644 actions/src/remote_actions/mapping_manage/edit_mapping.rs delete mode 100644 actions/src/remote_actions/mapping_manage/merge_share_mapping.rs delete mode 100644 actions/src/remote_actions/mapping_manage/share_mapping.rs delete mode 100644 actions/src/remote_actions/sheet_manage.rs delete mode 100644 actions/src/remote_actions/sheet_manage/drop_sheet.rs delete mode 100644 actions/src/remote_actions/sheet_manage/make_sheet.rs delete mode 100644 actions/src/remote_actions/workspace_manage.rs delete mode 100644 actions/src/remote_actions/workspace_manage/set_upstream_vault.rs delete mode 100644 actions/src/remote_actions/workspace_manage/update_to_latest_info.rs delete mode 100644 data/Cargo.toml delete mode 100644 data/src/constants.rs delete mode 100644 data/src/data.rs delete mode 100644 data/src/data/local.rs delete mode 100644 data/src/data/local/align_tasks.rs delete mode 100644 data/src/data/local/cached_sheet.rs delete mode 100644 data/src/data/local/latest_file_data.rs delete mode 100644 data/src/data/local/latest_info.rs delete mode 100644 data/src/data/local/local_files.rs delete mode 100644 data/src/data/local/local_sheet.rs delete mode 100644 data/src/data/local/modified_status.rs delete mode 100644 data/src/data/local/workspace_analyzer.rs delete mode 100644 data/src/data/local/workspace_config.rs delete mode 100644 data/src/data/member.rs delete mode 100644 data/src/data/sheet.rs delete mode 100644 data/src/data/user.rs delete mode 100644 data/src/data/user/accounts.rs delete mode 100644 data/src/data/vault.rs delete mode 100644 data/src/data/vault/lock_status.rs delete mode 100644 data/src/data/vault/mapping_share.rs delete mode 100644 data/src/data/vault/member_manage.rs delete mode 100644 data/src/data/vault/sheet_manage.rs delete mode 100644 data/src/data/vault/vault_config.rs delete mode 100644 data/src/data/vault/virtual_file.rs delete mode 100644 data/src/env.rs delete mode 100644 data/src/lib.rs delete mode 100644 data/tests/Cargo.toml delete mode 100644 data/tests/src/lib.rs delete mode 100644 data/tests/src/test_local_workspace_setup_and_account_management.rs delete mode 100644 data/tests/src/test_sheet_creation_management_and_persistence.rs delete mode 100644 data/tests/src/test_sheet_share_creation_and_management.rs delete mode 100644 data/tests/src/test_vault_setup_and_member_register.rs delete mode 100644 data/tests/src/test_virtual_file_creation_and_update.rs delete mode 100644 examples/Cargo.toml delete mode 100644 examples/src/bin/example_action_system.rs delete mode 100644 examples/src/lib.rs create mode 100644 legacy_actions/Cargo.toml create mode 100644 legacy_actions/src/connection.rs create mode 100644 legacy_actions/src/connection/action_service.rs create mode 100644 legacy_actions/src/connection/error.rs create mode 100644 legacy_actions/src/connection/protocol.rs create mode 100644 legacy_actions/src/lib.rs create mode 100644 legacy_actions/src/local_actions.rs create mode 100644 legacy_actions/src/local_actions/account_manage.rs create mode 100644 legacy_actions/src/local_actions/account_manage/register_account.rs create mode 100644 legacy_actions/src/local_actions/account_manage/remove_account.rs create mode 100644 legacy_actions/src/local_actions/account_manage/switch_account.rs create mode 100644 legacy_actions/src/local_actions/current_sheet.rs create mode 100644 legacy_actions/src/local_actions/current_sheet/exit_sheet.rs create mode 100644 legacy_actions/src/local_actions/current_sheet/use_sheet.rs create mode 100644 legacy_actions/src/registry.rs create mode 100644 legacy_actions/src/registry/client_registry.rs create mode 100644 legacy_actions/src/registry/server_registry.rs create mode 100644 legacy_actions/src/remote_actions.rs create mode 100644 legacy_actions/src/remote_actions/content_manage.rs create mode 100644 legacy_actions/src/remote_actions/content_manage/track_file.rs create mode 100644 legacy_actions/src/remote_actions/edit_right_manage.rs create mode 100644 legacy_actions/src/remote_actions/edit_right_manage/change_virtual_file_edit_right.rs create mode 100644 legacy_actions/src/remote_actions/mapping_manage.rs create mode 100644 legacy_actions/src/remote_actions/mapping_manage/edit_mapping.rs create mode 100644 legacy_actions/src/remote_actions/mapping_manage/merge_share_mapping.rs create mode 100644 legacy_actions/src/remote_actions/mapping_manage/share_mapping.rs create mode 100644 legacy_actions/src/remote_actions/sheet_manage.rs create mode 100644 legacy_actions/src/remote_actions/sheet_manage/drop_sheet.rs create mode 100644 legacy_actions/src/remote_actions/sheet_manage/make_sheet.rs create mode 100644 legacy_actions/src/remote_actions/workspace_manage.rs create mode 100644 legacy_actions/src/remote_actions/workspace_manage/set_upstream_vault.rs create mode 100644 legacy_actions/src/remote_actions/workspace_manage/update_to_latest_info.rs create mode 100644 legacy_data/Cargo.toml create mode 100644 legacy_data/src/constants.rs create mode 100644 legacy_data/src/data.rs create mode 100644 legacy_data/src/data/local.rs create mode 100644 legacy_data/src/data/local/align_tasks.rs create mode 100644 legacy_data/src/data/local/cached_sheet.rs create mode 100644 legacy_data/src/data/local/latest_file_data.rs create mode 100644 legacy_data/src/data/local/latest_info.rs create mode 100644 legacy_data/src/data/local/local_files.rs create mode 100644 legacy_data/src/data/local/local_sheet.rs create mode 100644 legacy_data/src/data/local/modified_status.rs create mode 100644 legacy_data/src/data/local/workspace_analyzer.rs create mode 100644 legacy_data/src/data/local/workspace_config.rs create mode 100644 legacy_data/src/data/member.rs create mode 100644 legacy_data/src/data/sheet.rs create mode 100644 legacy_data/src/data/user.rs create mode 100644 legacy_data/src/data/user/accounts.rs create mode 100644 legacy_data/src/data/vault.rs create mode 100644 legacy_data/src/data/vault/lock_status.rs create mode 100644 legacy_data/src/data/vault/mapping_share.rs create mode 100644 legacy_data/src/data/vault/member_manage.rs create mode 100644 legacy_data/src/data/vault/sheet_manage.rs create mode 100644 legacy_data/src/data/vault/vault_config.rs create mode 100644 legacy_data/src/data/vault/virtual_file.rs create mode 100644 legacy_data/src/env.rs create mode 100644 legacy_data/src/lib.rs create mode 100644 legacy_data/tests/Cargo.toml create mode 100644 legacy_data/tests/src/lib.rs create mode 100644 legacy_data/tests/src/test_local_workspace_setup_and_account_management.rs create mode 100644 legacy_data/tests/src/test_sheet_creation_management_and_persistence.rs create mode 100644 legacy_data/tests/src/test_sheet_share_creation_and_management.rs create mode 100644 legacy_data/tests/src/test_vault_setup_and_member_register.rs create mode 100644 legacy_data/tests/src/test_virtual_file_creation_and_update.rs create mode 100644 scripts/dev/setup.ps1 create mode 100644 scripts/dev/setup.sh delete mode 100644 setup.ps1 delete mode 100755 setup.sh diff --git a/.gitignore b/.gitignore index aa7aaa7..c04ba95 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ **/mutants.out*/ **/.temp/ +*[T|t]odo*.txt + ######################## ### IGNORE DIRECTORY ### ######################## diff --git a/Cargo.lock b/Cargo.lock index 41087f2..adaca97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,19 +446,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" -[[package]] -name = "examples" -version = "0.1.0" -dependencies = [ - "action_system", - "cfg_file", - "serde", - "serde_json", - "string_proc", - "tcp_connection", - "tokio", -] - [[package]] name = "fiat-crypto" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index be67728..f0274c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "just_enough_vcs" edition = "2024" -license-file = "LICENSE-MIT.md" +license-file = "LICENSE-MIT" authors = ["Weicao-CatilGrass (GitHub)"] [features] @@ -11,19 +11,17 @@ all = [ "sha1_hash", "tcp_connection", "string_proc", - "vcs", + "lib", ] cfg_file = [] data_struct = [] sha1_hash = [] tcp_connection = [] string_proc = [] -vcs = [] +lib = [] [workspace] members = [ - "examples", - "utils/cfg_file", "utils/cfg_file/cfg_file_derive", "utils/cfg_file/cfg_file_test", @@ -40,10 +38,10 @@ members = [ "systems/action", "systems/action/action_macros", - "data", - "data/tests", + "legacy_data", + "legacy_data/tests", - "actions", + "legacy_actions", "docs", ] @@ -84,5 +82,5 @@ string_proc = { path = "utils/string_proc" } action_system = { path = "systems/action" } vcs_docs = { path = "docs" } -vcs_data = { path = "data" } -vcs_actions = { path = "actions" } +vcs_data = { path = "legacy_data" } +vcs_actions = { path = "legacy_actions" } diff --git a/actions/Cargo.toml b/actions/Cargo.toml deleted file mode 100644 index 615d3af..0000000 --- a/actions/Cargo.toml +++ /dev/null @@ -1,29 +0,0 @@ -[package] -name = "vcs_actions" -edition = "2024" -version.workspace = true - -[dependencies] - -# Utils -tcp_connection = { path = "../utils/tcp_connection" } -cfg_file = { path = "../utils/cfg_file", features = ["default"] } -sha1_hash = { path = "../utils/sha1_hash" } -string_proc = { path = "../utils/string_proc" } - -# Core dependencies -action_system = { path = "../systems/action" } -vcs_data = { path = "../data" } - -# Error handling -thiserror = "2.0.17" - -# Serialization -serde = { version = "1.0.228", features = ["derive"] } -serde_json = "1.0.145" - -# Async & Networking -tokio = { version = "1.48.0", features = ["full"] } - -# Logging -log = "0.4.28" diff --git a/actions/src/connection.rs b/actions/src/connection.rs deleted file mode 100644 index 918f93c..0000000 --- a/actions/src/connection.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod action_service; -pub mod error; -pub mod protocol; diff --git a/actions/src/connection/action_service.rs b/actions/src/connection/action_service.rs deleted file mode 100644 index 3e05a70..0000000 --- a/actions/src/connection/action_service.rs +++ /dev/null @@ -1,221 +0,0 @@ -use std::{ - env::set_current_dir, - net::SocketAddr, - path::PathBuf, - sync::Arc, - time::{Duration, Instant}, -}; - -use action_system::{action::ActionContext, action_pool::ActionPool}; -use cfg_file::config::ConfigFile; -use log::{debug, error, info, warn}; -use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance}; -use tokio::{ - net::{TcpListener, TcpStream}, - select, signal, spawn, - sync::mpsc, -}; -use vcs_data::data::vault::{Vault, vault_config::VaultConfig}; - -use crate::{ - connection::protocol::RemoteActionInvoke, registry::server_registry::server_action_pool, -}; - -// Start the server with a Vault using the specified directory -pub async fn server_entry( - vault_path: impl Into, - port_override: u16, -) -> Result<(), TcpTargetError> { - let vault_path = vault_path.into(); - - // Set to vault path - set_current_dir(&vault_path).map_err(|e| TcpTargetError::Io(e.to_string()))?; - - // Read the vault cfg - let vault_cfg = VaultConfig::read().await?; - - // Create TCPListener - let listener = create_tcp_listener(&vault_cfg, port_override).await?; - - // Initialize the vault - let vault: Arc = init_vault(vault_cfg, vault_path).await?; - - // Lock the vault - vault - .lock() - .map_err(|e| TcpTargetError::Locked(e.to_string()))?; - - // Create ActionPool - let action_pool: Arc = Arc::new(server_action_pool()); - - // Start the server - let (_shutdown_rx, future) = build_server_future(vault.clone(), action_pool.clone(), listener); - future.await?; // Start and block until shutdown - - // Unlock the vault - vault.unlock()?; - - Ok(()) -} - -async fn create_tcp_listener( - cfg: &VaultConfig, - port_override: u16, -) -> Result { - let local_bind_addr = cfg.server_config().local_bind(); - let port = if port_override > 0 { - port_override // Override -> PORT > 0 - } else { - cfg.server_config().port() // Default -> Port = 0 - }; - let bind_port = port; - let sock_addr = SocketAddr::new(*local_bind_addr, bind_port); - let listener = TcpListener::bind(sock_addr).await?; - - Ok(listener) -} - -async fn init_vault(cfg: VaultConfig, path: PathBuf) -> Result, TcpTargetError> { - // Init and create the vault - let Some(vault) = Vault::init(cfg, path) else { - return Err(TcpTargetError::NotFound("Vault not found".to_string())); - }; - let vault: Arc = Arc::new(vault); - - Ok(vault) -} - -fn build_server_future( - vault: Arc, - action_pool: Arc, - listener: TcpListener, -) -> ( - mpsc::Sender<()>, - impl std::future::Future>, -) { - let (tx, mut rx) = mpsc::channel::(100); - let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(1); - let mut active_connections = 0; - let mut shutdown_requested = false; - - // Spawn task to handle Ctrl+C with rapid exit detection - let shutdown_tx_clone = shutdown_tx.clone(); - spawn(async move { - let mut ctrl_c_count = 0; - let mut last_ctrl_c_time = Instant::now(); - - while let Ok(()) = signal::ctrl_c().await { - let now = Instant::now(); - - // Reset counter if more than 5 seconds have passed - if now.duration_since(last_ctrl_c_time) > Duration::from_secs(5) { - ctrl_c_count = 0; - } - - ctrl_c_count += 1; - last_ctrl_c_time = now; - - let _ = shutdown_tx_clone.send(()).await; - - // If 3 Ctrl+C within 5 seconds, exit immediately - if ctrl_c_count >= 3 { - info!("Shutdown. (3/3)"); - std::process::exit(0); - } else { - info!("Ctrl + C to force shutdown. ({} / 3)", ctrl_c_count); - } - } - }); - - let future = async move { - loop { - select! { - // Accept new connections - accept_result = listener.accept(), if !shutdown_requested => { - match accept_result { - Ok((stream, _addr)) => { - debug!("New connection. (now {})", active_connections); - let _ = tx.send(1).await; - - let vault_clone = vault.clone(); - let action_pool_clone = action_pool.clone(); - let tx_clone = tx.clone(); - - spawn(async move { - process_connection(stream, vault_clone, action_pool_clone).await; - debug!("A connection closed. (now {})", active_connections); - let _ = tx_clone.send(-1).await; - }); - } - Err(_) => { - continue; - } - } - } - - // Handle connection count updates - Some(count_change) = rx.recv() => { - active_connections = (active_connections as i32 + count_change) as usize; - - // Check if we should shutdown after all connections are done - if shutdown_requested && active_connections == 0 { - break; - } - } - - // Handle shutdown signal - _ = shutdown_rx.recv() => { - shutdown_requested = true; - // If no active connections, break immediately - if active_connections == 0 { - info!("No active connections. Shutting down."); - break; - } else { - warn!("Cannot shutdown while active connections exist! ({} active)", active_connections); - } - } - } - } - - Ok(()) - }; - - (shutdown_tx, future) -} - -async fn process_connection(stream: TcpStream, vault: Arc, action_pool: Arc) { - // Setup connection instance - let mut instance = ConnectionInstance::from(stream); - - // Read action name and action arguments - let msg = match instance.read_msgpack::().await { - Ok(msg) => msg, - Err(e) => { - error!("Failed to read action message: {}", e); - return; - } - }; - - // Build context - let ctx: ActionContext = ActionContext::remote().insert_instance(instance); - - // Insert vault into context - let ctx = ctx.with_arc_data(vault); - - info!( - "Process action `{}` with argument `{}`", - msg.action_name, msg.action_args_json - ); - - // Process action - let result = action_pool - .process_json(&msg.action_name, ctx, msg.action_args_json) - .await; - - match result { - Ok(_result_json) => {} - Err(e) => { - warn!("Failed to process action `{}`: {}", msg.action_name, e); - } - } -} diff --git a/actions/src/connection/error.rs b/actions/src/connection/error.rs deleted file mode 100644 index 1a4e221..0000000 --- a/actions/src/connection/error.rs +++ /dev/null @@ -1,27 +0,0 @@ -use std::io; -use thiserror::Error; -use vcs_data::data::member::MemberId; - -#[derive(Error, Debug, Clone)] -pub enum ConnectionError { - #[error("I/O error: {0}")] - Io(String), -} - -#[derive(Error, Debug, Clone)] -pub enum ProcessActionError { - #[error("Action `{0}` not registered")] - ActionNotRegistered(String), - - #[error("Authorize `{0}` failed")] - AuthorizeFailed(MemberId), - - #[error("Authorize host `{0}` failed")] - AuthorizeHostFailed(MemberId), -} - -impl From for ConnectionError { - fn from(error: io::Error) -> Self { - ConnectionError::Io(error.to_string()) - } -} diff --git a/actions/src/connection/protocol.rs b/actions/src/connection/protocol.rs deleted file mode 100644 index 2cebe79..0000000 --- a/actions/src/connection/protocol.rs +++ /dev/null @@ -1,7 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Default, Clone, Serialize, Deserialize)] -pub struct RemoteActionInvoke { - pub action_name: String, - pub action_args_json: String, -} diff --git a/actions/src/lib.rs b/actions/src/lib.rs deleted file mode 100644 index c1dda86..0000000 --- a/actions/src/lib.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub mod connection; -pub mod local_actions; -pub mod registry; -pub mod remote_actions; diff --git a/actions/src/local_actions.rs b/actions/src/local_actions.rs deleted file mode 100644 index d6f47f9..0000000 --- a/actions/src/local_actions.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod account_manage; -pub mod current_sheet; diff --git a/actions/src/local_actions/account_manage.rs b/actions/src/local_actions/account_manage.rs deleted file mode 100644 index 03a7851..0000000 --- a/actions/src/local_actions/account_manage.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod register_account; -pub mod remove_account; -pub mod switch_account; diff --git a/actions/src/local_actions/account_manage/register_account.rs b/actions/src/local_actions/account_manage/register_account.rs deleted file mode 100644 index e69de29..0000000 diff --git a/actions/src/local_actions/account_manage/remove_account.rs b/actions/src/local_actions/account_manage/remove_account.rs deleted file mode 100644 index e69de29..0000000 diff --git a/actions/src/local_actions/account_manage/switch_account.rs b/actions/src/local_actions/account_manage/switch_account.rs deleted file mode 100644 index e69de29..0000000 diff --git a/actions/src/local_actions/current_sheet.rs b/actions/src/local_actions/current_sheet.rs deleted file mode 100644 index 785d7ee..0000000 --- a/actions/src/local_actions/current_sheet.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod exit_sheet; -pub mod use_sheet; diff --git a/actions/src/local_actions/current_sheet/exit_sheet.rs b/actions/src/local_actions/current_sheet/exit_sheet.rs deleted file mode 100644 index e69de29..0000000 diff --git a/actions/src/local_actions/current_sheet/use_sheet.rs b/actions/src/local_actions/current_sheet/use_sheet.rs deleted file mode 100644 index e69de29..0000000 diff --git a/actions/src/registry.rs b/actions/src/registry.rs deleted file mode 100644 index ceec1a1..0000000 --- a/actions/src/registry.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod client_registry; -pub mod server_registry; diff --git a/actions/src/registry/client_registry.rs b/actions/src/registry/client_registry.rs deleted file mode 100644 index db69889..0000000 --- a/actions/src/registry/client_registry.rs +++ /dev/null @@ -1,128 +0,0 @@ -use std::sync::Arc; - -use action_system::{action::ActionContext, action_pool::ActionPool}; -use cfg_file::config::ConfigFile; -use tcp_connection::error::TcpTargetError; -use vcs_data::data::{ - local::{LocalWorkspace, workspace_config::LocalConfig}, - user::UserDirectory, -}; - -use crate::{ - connection::protocol::RemoteActionInvoke, - remote_actions::{ - content_manage::track_file::register_track_file_action, - edit_right_manage::change_virtual_file_edit_right::register_change_virtual_file_edit_right_action, - mapping_manage::{ - edit_mapping::register_edit_mapping_action, - merge_share_mapping::register_merge_share_mapping_action, - share_mapping::register_share_mapping_action, - }, - sheet_manage::{ - drop_sheet::register_drop_sheet_action, make_sheet::register_make_sheet_action, - }, - workspace_manage::{ - set_upstream_vault::register_set_upstream_vault_action, - update_to_latest_info::register_update_to_latest_info_action, - }, - }, -}; - -fn register_actions(pool: &mut ActionPool) { - // Pool register here - - // Local Actions - register_set_upstream_vault_action(pool); - register_update_to_latest_info_action(pool); - - // Sheet Actions - register_make_sheet_action(pool); - register_drop_sheet_action(pool); - register_edit_mapping_action(pool); - - // Share / Merge Share Actions - register_share_mapping_action(pool); - register_merge_share_mapping_action(pool); - - // Track Action - register_track_file_action(pool); - - // User Actions - register_change_virtual_file_edit_right_action(pool); -} - -pub fn client_action_pool() -> ActionPool { - // Create pool - let mut pool = ActionPool::new(); - - // Register actions - register_actions(&mut pool); - - // Add process events - pool.set_on_proc_begin(|ctx, args| Box::pin(on_proc_begin(ctx, args))); - - // Return - pool -} - -async fn on_proc_begin( - ctx: &mut ActionContext, - _args: &(dyn std::any::Any + Send + Sync), -) -> Result<(), TcpTargetError> { - // Is ctx remote - let is_remote = ctx.is_remote_action(); - - // Action name and arguments - let action_name = ctx.action_name().to_string(); - let action_args_json = ctx.action_args_json().clone(); - - // Insert LocalWorkspace Arc - let Ok(local_config) = LocalConfig::read().await else { - return Err(TcpTargetError::NotFound( - "The current directory does not have a local workspace".to_string(), - )); - }; - let local_workspace = match LocalWorkspace::init_current_dir(local_config) { - Some(workspace) => workspace, - None => { - return Err(TcpTargetError::NotFound( - "Failed to initialize local workspace.".to_string(), - )); - } - }; - let local_workspace_arc = Arc::new(local_workspace); - ctx.insert_arc_data(local_workspace_arc); - - // Insert UserDirectory Arc - let Some(user_directory) = UserDirectory::current_cfg_dir() else { - return Err(TcpTargetError::NotFound( - "The user directory does not exist.".to_string(), - )); - }; - - let user_directory_arc = Arc::new(user_directory); - ctx.insert_arc_data(user_directory_arc); - - // Get instance - let Some(instance) = ctx.instance() else { - return Err(TcpTargetError::Unsupported( - "Missing ConnectionInstance in current context, this ActionPool does not support this call" - .to_string())); - }; - - // If it's remote, invoke action at server - if is_remote { - // Build protocol message - let msg = RemoteActionInvoke { - action_name, - action_args_json, - }; - - // Send - let mut instance = instance.lock().await; - instance.write_msgpack(&msg).await?; - } - - // Return OK, wait for client to execute Action locally - Ok(()) -} diff --git a/actions/src/registry/server_registry.rs b/actions/src/registry/server_registry.rs deleted file mode 100644 index aee867c..0000000 --- a/actions/src/registry/server_registry.rs +++ /dev/null @@ -1,43 +0,0 @@ -use action_system::action_pool::ActionPool; - -use crate::remote_actions::{ - content_manage::track_file::register_track_file_action, - edit_right_manage::change_virtual_file_edit_right::register_change_virtual_file_edit_right_action, - mapping_manage::{ - edit_mapping::register_edit_mapping_action, - merge_share_mapping::register_merge_share_mapping_action, - share_mapping::register_share_mapping_action, - }, - sheet_manage::{ - drop_sheet::register_drop_sheet_action, make_sheet::register_make_sheet_action, - }, - workspace_manage::{ - set_upstream_vault::register_set_upstream_vault_action, - update_to_latest_info::register_update_to_latest_info_action, - }, -}; - -pub fn server_action_pool() -> ActionPool { - let mut pool = ActionPool::new(); - - // Local Actions - register_set_upstream_vault_action(&mut pool); - register_update_to_latest_info_action(&mut pool); - - // Sheet Actions - register_make_sheet_action(&mut pool); - register_drop_sheet_action(&mut pool); - register_edit_mapping_action(&mut pool); - - // Share / Merge Share Actions - register_share_mapping_action(&mut pool); - register_merge_share_mapping_action(&mut pool); - - // Track Action - register_track_file_action(&mut pool); - - // User Actions - register_change_virtual_file_edit_right_action(&mut pool); - - pool -} diff --git a/actions/src/remote_actions.rs b/actions/src/remote_actions.rs deleted file mode 100644 index d15edc9..0000000 --- a/actions/src/remote_actions.rs +++ /dev/null @@ -1,288 +0,0 @@ -use std::sync::Arc; - -use action_system::action::ActionContext; -use cfg_file::config::ConfigFile; -use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance}; -use tokio::sync::{Mutex, mpsc::Sender}; -use vcs_data::{ - constants::{SERVER_PATH_MEMBER_PUB, VAULT_HOST_NAME}, - data::{ - local::{LocalWorkspace, latest_info::LatestInfo, workspace_config::LocalConfig}, - member::MemberId, - sheet::SheetName, - user::UserDirectory, - vault::Vault, - }, -}; - -pub mod content_manage; -pub mod edit_right_manage; -pub mod mapping_manage; -pub mod sheet_manage; -pub mod workspace_manage; - -/// Check if the connection instance is valid in the given context. -/// This function is used to verify the connection instance in actions that require remote calls. -pub fn check_connection_instance( - ctx: &ActionContext, -) -> Result<&Arc>, TcpTargetError> { - let Some(instance) = ctx.instance() else { - return Err(TcpTargetError::NotFound( - "Connection instance lost.".to_string(), - )); - }; - Ok(instance) -} - -/// Try to get the Vault instance from the context. -pub fn try_get_vault(ctx: &ActionContext) -> Result, TcpTargetError> { - let Some(vault) = ctx.get_arc::() else { - return Err(TcpTargetError::NotFound( - "Vault instance not found".to_string(), - )); - }; - Ok(vault) -} - -/// Try to get the LocalWorkspace instance from the context. -pub fn try_get_local_workspace(ctx: &ActionContext) -> Result, TcpTargetError> { - let Some(local_workspace) = ctx.get_arc::() else { - return Err(TcpTargetError::NotFound( - "LocalWorkspace instance not found".to_string(), - )); - }; - Ok(local_workspace) -} - -/// Try to get the UserDirectory instance from the context. -pub fn try_get_user_directory(ctx: &ActionContext) -> Result, TcpTargetError> { - let Some(user_directory) = ctx.get_arc::() else { - return Err(TcpTargetError::NotFound( - "UserDirectory instance not found".to_string(), - )); - }; - Ok(user_directory) -} - -/// Try to get the LocalWorkspace instance from the context. -pub fn try_get_local_output(ctx: &ActionContext) -> Result>, TcpTargetError> { - let Some(output) = ctx.get_arc::>() else { - return Err(TcpTargetError::NotFound( - "Client sender not found".to_string(), - )); - }; - Ok(output) -} - -/// Authenticate member based on context and return MemberId -pub async fn auth_member( - ctx: &ActionContext, - instance: &Arc>, -) -> Result<(MemberId, bool), TcpTargetError> { - // Window开服Linux连接 -> 此函数内产生 early eof - // ~ WS # jv update - // 身份认证失败:I/O error: early eof! - - // 分析相应流程: - // 1. 服务端发起挑战,客户端接受 - // 2. 服务端发送结果,客户端接受 - // 3. 推测此时发生 early eof ---> 无 ack,导致客户端尝试拿到结果时,服务端已经结束 - // 这很有可能是 Windows 和 Linux 对于连接处理的方案差异导致的问题,需要进一步排查 - - // Start Challenge (Remote) - if ctx.is_proc_on_remote() { - let mut mut_instance = instance.lock().await; - let vault = try_get_vault(ctx)?; - - let using_host_mode = mut_instance.read_msgpack::().await?; - - let result = mut_instance - .challenge(vault.vault_path().join(SERVER_PATH_MEMBER_PUB)) - .await; - - return match result { - Ok((pass, member_id)) => { - if !pass { - // Send false to inform the client that authentication failed - mut_instance.write(false).await?; - Err(TcpTargetError::Authentication( - "Authenticate failed.".to_string(), - )) - } else { - if using_host_mode { - if vault.config().vault_host_list().contains(&member_id) { - // Using Host mode authentication, and is indeed an administrator - mut_instance.write(true).await?; - Ok((member_id, true)) - } else { - // Using Host mode authentication, but not an administrator - mut_instance.write(false).await?; - Err(TcpTargetError::Authentication( - "Authenticate failed.".to_string(), - )) - } - } else { - // Not using Host mode authentication - mut_instance.write(true).await?; - Ok((member_id, false)) - } - } - } - Err(e) => Err(e), - }; - } - - // Accept Challenge (Local) - if ctx.is_proc_on_local() { - let mut mut_instance = instance.lock().await; - let local_workspace = try_get_local_workspace(ctx)?; - let (is_host_mode, member_name) = { - let cfg = local_workspace.config().lock_owned().await; - (cfg.is_host_mode(), cfg.current_account()) - }; - let user_directory = try_get_user_directory(ctx)?; - - // Inform remote whether to authenticate in Host mode - mut_instance.write_msgpack(is_host_mode).await?; - - // Member name & Private key - let private_key = user_directory.account_private_key_path(&member_name); - let _ = mut_instance - .accept_challenge(private_key, &member_name) - .await?; - - // Read result - let challenge_result = mut_instance.read::().await?; - if challenge_result { - return Ok((member_name.clone(), is_host_mode)); - } else { - return Err(TcpTargetError::Authentication( - "Authenticate failed.".to_string(), - )); - } - } - - Err(TcpTargetError::NoResult("Auth failed.".to_string())) -} - -/// Get the current sheet name based on the context (local or remote). -/// This function handles the communication between local and remote instances -/// to verify and retrieve the current sheet name and whether it's a reference sheet. -/// -/// On local: -/// - Reads the current sheet from local configuration -/// - Sends the sheet name to remote for verification -/// - Returns the sheet name and whether it's a reference sheet if remote confirms it exists -/// -/// On remote: -/// - Receives sheet name from local -/// - Verifies the sheet exists in the vault -/// - Checks if the sheet is a reference sheet -/// - If allow_ref is true, reference sheets are allowed to pass verification -/// - Sends confirmation and reference status back to local -/// -/// Returns a tuple of (SheetName, bool) where the bool indicates if it's a reference sheet, -/// or an error if the sheet doesn't exist or doesn't meet the verification criteria. -pub async fn get_current_sheet_name( - ctx: &ActionContext, - instance: &Arc>, - member_id: &MemberId, - allow_ref: bool, -) -> Result<(SheetName, bool), TcpTargetError> { - let mut mut_instance = instance.lock().await; - if ctx.is_proc_on_local() { - let workspace = try_get_local_workspace(ctx)?; - let config = LocalConfig::read().await?; - let latest = LatestInfo::read_from(LatestInfo::latest_info_path( - workspace.local_path(), - member_id, - )) - .await?; - if let Some(sheet_name) = config.sheet_in_use() { - // Send sheet name - mut_instance.write_msgpack(sheet_name).await?; - - // Read result - if mut_instance.read_msgpack::().await? { - // Check if sheet is a reference sheet - let is_ref_sheet = latest.reference_sheets.contains(sheet_name); - if allow_ref { - // Allow reference sheets, directly return the determination result - return Ok((sheet_name.clone(), is_ref_sheet)); - } else if is_ref_sheet { - // Not allowed but it's a reference sheet, return an error - return Err(TcpTargetError::ReferenceSheetNotAllowed( - "Reference sheet not allowed".to_string(), - )); - } else { - // Not allowed but not a reference sheet, return normally - return Ok((sheet_name.clone(), false)); - } - } else { - return Err(TcpTargetError::NotFound("Sheet not found".to_string())); - } - } - // Send empty sheet_name - mut_instance.write_msgpack("".to_string()).await?; - - // Read result, since we know it's impossible to pass here, we just consume this result - let _ = mut_instance.read_msgpack::().await?; - - return Err(TcpTargetError::NotFound("Sheet not found".to_string())); - } - if ctx.is_proc_on_remote() { - let vault = try_get_vault(ctx)?; - - // Read sheet name - let sheet_name: SheetName = mut_instance.read_msgpack().await?; - - // Check if sheet exists - if let Ok(sheet) = vault.sheet(&sheet_name).await - && let Some(holder) = sheet.holder() - { - let is_ref_sheet = holder == VAULT_HOST_NAME; - if allow_ref { - // Allow reference sheets, directly return the determination result - if holder == member_id || holder == VAULT_HOST_NAME { - mut_instance.write_msgpack(true).await?; - return Ok((sheet.name().clone(), is_ref_sheet)); - } - } else if is_ref_sheet { - // Not allowed but it's a reference sheet, return an error - mut_instance.write_msgpack(true).await?; - return Err(TcpTargetError::ReferenceSheetNotAllowed( - "Reference sheet not allowed".to_string(), - )); - } else { - // Not allowed but not a reference sheet, return normally - if holder == member_id { - mut_instance.write_msgpack(true).await?; - return Ok((sheet_name.clone(), false)); - } - } - } - // Tell local the check is not passed - mut_instance.write_msgpack(false).await?; - return Err(TcpTargetError::NotFound("Sheet not found".to_string())); - } - Err(TcpTargetError::NoResult("NoResult".to_string())) -} - -/// The macro to write and return a result. -#[macro_export] -macro_rules! write_and_return { - ($instance:expr, $result:expr) => {{ - $instance.lock().await.write($result).await?; - return Ok($result); - }}; -} - -/// The macro to send formatted string to output channel. -/// Usage: local_println!(output, "format string", arg1, arg2, ...) -#[macro_export] -macro_rules! local_println { - ($output:expr, $($arg:tt)*) => {{ - let formatted = format!($($arg)*); - let _ = $output.send(formatted).await; - }}; -} diff --git a/actions/src/remote_actions/content_manage.rs b/actions/src/remote_actions/content_manage.rs deleted file mode 100644 index 1568085..0000000 --- a/actions/src/remote_actions/content_manage.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod track_file; diff --git a/actions/src/remote_actions/content_manage/track_file.rs b/actions/src/remote_actions/content_manage/track_file.rs deleted file mode 100644 index a59ca76..0000000 --- a/actions/src/remote_actions/content_manage/track_file.rs +++ /dev/null @@ -1,985 +0,0 @@ -use std::{ - collections::{HashMap, HashSet}, - path::PathBuf, - sync::Arc, - time::SystemTime, -}; - -use action_system::{action::ActionContext, macros::action_gen}; -use cfg_file::config::ConfigFile; -use serde::{Deserialize, Serialize}; -use sha1_hash::calc_sha1; -use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance}; -use tokio::{fs, sync::Mutex}; -use vcs_data::{ - constants::{CLIENT_FILE_TEMP_FILE, KEY_TEMP_NAME}, - data::{ - local::{ - cached_sheet::CachedSheet, latest_file_data::LatestFileData, - local_sheet::LocalMappingMetadata, modified_status::sign_vault_modified, - workspace_analyzer::AnalyzeResult, - }, - member::MemberId, - sheet::SheetName, - vault::{ - vault_config::VaultUuid, - virtual_file::{VirtualFileId, VirtualFileVersion, VirtualFileVersionDescription}, - }, - }, -}; - -use crate::{ - local_println, - remote_actions::{ - auth_member, check_connection_instance, get_current_sheet_name, try_get_local_output, - try_get_local_workspace, try_get_vault, - }, -}; - -pub type NextVersion = String; -pub type UpdateDescription = String; - -#[derive(Serialize, Deserialize)] -pub struct TrackFileActionArguments { - // Path need to track - pub relative_pathes: HashSet, - - // File update info - pub file_update_info: HashMap, - - // Print infos - pub print_infos: bool, - - // overwrite modified files - pub allow_overwrite_modified: bool, -} - -#[derive(Serialize, Deserialize)] -pub enum TrackFileActionResult { - Done { - created: Vec, - updated: Vec, - synced: Vec, - skipped: Vec, - }, - - // Fail - AuthorizeFailed(String), - - /// There are local move or missing items that have not been resolved, - /// this situation does not allow track - StructureChangesNotSolved, - - CreateTaskFailed(CreateTaskResult), - UpdateTaskFailed(UpdateTaskResult), - SyncTaskFailed(SyncTaskResult), -} - -#[derive(Serialize, Deserialize)] -pub enum CreateTaskResult { - Success(Vec), // Success(success_relative_pathes) - - /// Create file on existing path in the sheet - CreateFileOnExistPath(PathBuf), - - /// Sheet not found - SheetNotFound(SheetName), -} - -#[derive(Serialize, Deserialize)] -pub enum UpdateTaskResult { - Success(Vec), // Success(success_relative_pathes) - - VerifyFailed { - path: PathBuf, - reason: VerifyFailReason, - }, -} - -#[derive(Serialize, Deserialize, Clone)] -pub enum VerifyFailReason { - SheetNotFound(SheetName), - MappingNotFound, - VirtualFileNotFound(VirtualFileId), - VirtualFileReadFailed(VirtualFileId), - NotHeld, - VersionDismatch(VirtualFileVersion, VirtualFileVersion), // (CurrentVersion, RemoteVersion) - UpdateButNoDescription, // File needs update, but no description exists - VersionAlreadyExist(VirtualFileVersion), // (RemoteVersion) -} - -#[derive(Serialize, Deserialize)] -pub enum SyncTaskResult { - Success(Vec), // Success(success_relative_pathes) -} -#[action_gen] -pub async fn track_file_action( - ctx: ActionContext, - arguments: TrackFileActionArguments, -) -> Result { - let relative_pathes = arguments.relative_pathes; - let instance = check_connection_instance(&ctx)?; - - // Auth Member - let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { - Ok(id) => id, - Err(e) => return Ok(TrackFileActionResult::AuthorizeFailed(e.to_string())), - }; - - // Check sheet - let (sheet_name, is_ref_sheet) = - get_current_sheet_name(&ctx, instance, &member_id, true).await?; - - // Can modify Sheet when not in reference sheet or in Host mode - let can_modify_sheet = !is_ref_sheet || is_host_mode; - - if ctx.is_proc_on_local() { - let workspace = try_get_local_workspace(&ctx)?; - let analyzed = AnalyzeResult::analyze_local_status(&workspace).await?; - let latest_file_data = - LatestFileData::read_from(LatestFileData::data_path(&member_id)?).await?; - - if !analyzed.lost.is_empty() || !analyzed.moved.is_empty() { - return Ok(TrackFileActionResult::StructureChangesNotSolved); - } - - let Some(sheet_in_use) = workspace.config().lock().await.sheet_in_use().clone() else { - return Err(TcpTargetError::NotFound("Sheet not found!".to_string())); - }; - - // Read local sheet and member held - let local_sheet = workspace.local_sheet(&member_id, &sheet_in_use).await?; - let cached_sheet = CachedSheet::cached_sheet_data(&sheet_in_use).await?; - let member_held = LatestFileData::read_from(LatestFileData::data_path(&member_id)?).await?; - - let modified = analyzed - .modified - .intersection(&relative_pathes) - .cloned() - .collect::>(); - - // Filter out created files - let created_task = analyzed - .created - .intersection(&relative_pathes) - .cloned() - .collect::>(); - - // Filter out modified files that need to be updated - let mut update_task: Vec = { - let result = modified.iter().filter_map(|p| { - if let Ok(local_data) = local_sheet.mapping_data(p) { - let id = local_data.mapping_vfid(); - let local_ver = local_data.version_when_updated(); - let Some(latest_ver) = latest_file_data.file_version(id) else { - return None; - }; - if let Some(held_member) = member_held.file_holder(id) { - // Check if holder and version match - if held_member == &member_id && local_ver == latest_ver { - return Some(p.clone()); - } - } - }; - None - }); - result.collect() - }; - - let mut skipped_task: Vec = Vec::new(); - - // Filter out files that do not exist locally or have version inconsistencies and need to be synchronized - let mut sync_task: Vec = { - let other: Vec = relative_pathes - .iter() - .filter(|p| !created_task.contains(p) && !update_task.contains(p)) - .cloned() - .collect(); - - let result = other.iter().filter_map(|p| { - // Not exists and not lost, first download - if !workspace.local_path().join(p).exists() && !analyzed.lost.contains(p) { - return Some(p.clone()); - } - - // In cached sheet - if !cached_sheet.mapping().contains_key(p) { - return None; - } - - // In local sheet - let local_sheet_mapping = local_sheet.mapping_data(p).ok()?; - let vfid = local_sheet_mapping.mapping_vfid(); - - if let Some(latest_version) = &latest_file_data.file_version(vfid) { - // Version does not match - if &local_sheet_mapping.version_when_updated() != latest_version { - let modified = modified.contains(p); - if modified && arguments.allow_overwrite_modified { - return Some(p.clone()); - } else if modified && !arguments.allow_overwrite_modified { - // If not allowed to overwrite, join skipped tasks - skipped_task.push(p.clone()); - return None; - } - return Some(p.clone()); - } - } - - // File not held and modified - let holder = latest_file_data.file_holder(vfid); - if (holder.is_none() || &member_id != holder.unwrap()) && modified.contains(p) { - // If allow overwrite modified is true, overwrite the file - if arguments.allow_overwrite_modified { - return Some(p.clone()); - } else { - // If not allowed to overwrite, join skipped tasks - skipped_task.push(p.clone()); - return None; - } - } - - None - }); - result.collect() - }; - - // If the sheet cannot be modified, - // the update_task here should be considered invalid and changed to sync rollback - if !can_modify_sheet { - if arguments.allow_overwrite_modified { - sync_task.append(&mut update_task); - update_task.clear(); - } else { - skipped_task.append(&mut update_task); - update_task.clear(); - } - } - - // Package tasks - let tasks: (Vec, Vec, Vec) = - (created_task, update_task, sync_task); - - // Send to remote - { - let mut mut_instance = instance.lock().await; - mut_instance - .write_large_msgpack(tasks.clone(), 1024u16) - .await?; - // Drop mutex here - } - - // Process create tasks - let mut success_create = Vec::::new(); - if can_modify_sheet { - success_create = match proc_create_tasks_local( - &ctx, - instance.clone(), - &member_id, - &sheet_name, - tasks.0, - arguments.print_infos, - ) - .await - { - Ok(r) => match r { - CreateTaskResult::Success(relative_pathes) => relative_pathes, - _ => { - return Ok(TrackFileActionResult::CreateTaskFailed(r)); - } - }, - Err(e) => return Err(e), - }; - } - - // Process update tasks - let mut success_update = Vec::::new(); - if can_modify_sheet { - success_update = match proc_update_tasks_local( - &ctx, - instance.clone(), - &member_id, - &sheet_name, - tasks.1, - arguments.print_infos, - arguments.file_update_info, - ) - .await - { - Ok(r) => match r { - UpdateTaskResult::Success(relative_pathes) => relative_pathes, - _ => { - return Ok(TrackFileActionResult::UpdateTaskFailed(r)); - } - }, - Err(e) => return Err(e), - }; - } - - // Process sync tasks - let success_sync = match proc_sync_tasks_local( - &ctx, - instance.clone(), - &member_id, - &sheet_name, - tasks.2, - arguments.print_infos, - ) - .await - { - Ok(r) => match r { - SyncTaskResult::Success(relative_pathes) => relative_pathes, - }, - Err(e) => return Err(e), - }; - - if success_create.len() + success_update.len() > 0 { - sign_vault_modified(true).await; - } - - return Ok(TrackFileActionResult::Done { - created: success_create, - updated: success_update, - synced: success_sync, - skipped: skipped_task, - }); - } - - if ctx.is_proc_on_remote() { - // Read tasks - let (created_task, update_task, sync_task): (Vec, Vec, Vec) = { - let mut mut_instance = instance.lock().await; - mut_instance.read_large_msgpack(1024u16).await? - }; - - // Process create tasks - let mut success_create = Vec::::new(); - if can_modify_sheet { - success_create = match proc_create_tasks_remote( - &ctx, - instance.clone(), - &member_id, - &sheet_name, - created_task, - ) - .await - { - Ok(r) => match r { - CreateTaskResult::Success(relative_pathes) => relative_pathes, - _ => { - return Ok(TrackFileActionResult::CreateTaskFailed(r)); - } - }, - Err(e) => return Err(e), - }; - } - - // Process update tasks - let mut success_update = Vec::::new(); - if can_modify_sheet { - success_update = match proc_update_tasks_remote( - &ctx, - instance.clone(), - &member_id, - &sheet_name, - update_task, - arguments.file_update_info, - ) - .await - { - Ok(r) => match r { - UpdateTaskResult::Success(relative_pathes) => relative_pathes, - _ => { - return Ok(TrackFileActionResult::UpdateTaskFailed(r)); - } - }, - Err(e) => return Err(e), - }; - } - - // Process sync tasks - let success_sync = match proc_sync_tasks_remote( - &ctx, - instance.clone(), - &member_id, - &sheet_name, - sync_task, - ) - .await - { - Ok(r) => match r { - SyncTaskResult::Success(relative_pathes) => relative_pathes, - }, - Err(e) => return Err(e), - }; - - return Ok(TrackFileActionResult::Done { - created: success_create, - updated: success_update, - synced: success_sync, - skipped: Vec::new(), // The server doesn't know which files were skipped - }); - } - - Err(TcpTargetError::NoResult("No result.".to_string())) -} - -async fn proc_create_tasks_local( - ctx: &ActionContext, - instance: Arc>, - member_id: &MemberId, - sheet_name: &SheetName, - relative_paths: Vec, - print_infos: bool, -) -> Result { - let workspace = try_get_local_workspace(ctx)?; - let local_output = try_get_local_output(ctx)?; - let mut mut_instance = instance.lock().await; - let mut local_sheet = workspace.local_sheet(member_id, sheet_name).await?; - - if print_infos && relative_paths.len() > 0 { - local_println!(local_output, "Creating {} files...", relative_paths.len()); - } - - // Wait for remote detection of whether the sheet exists - let has_sheet = mut_instance.read_msgpack::().await?; - if !has_sheet { - return Ok(CreateTaskResult::SheetNotFound(sheet_name.clone())); - } - - // Wait for remote detection of whether the file exists - let (hasnt_duplicate, duplicate_path) = mut_instance.read_msgpack::<(bool, PathBuf)>().await?; - if !hasnt_duplicate { - return Ok(CreateTaskResult::CreateFileOnExistPath(duplicate_path)); - } - - let mut success_relative_pathes = Vec::new(); - - // Start sending files - for path in relative_paths { - let full_path = workspace.local_path().join(&path); - - // Send file - if mut_instance.write_file(&full_path).await.is_err() { - continue; - } - - // Read virtual file id and version - let (vfid, version, version_desc) = mut_instance - .read_msgpack::<( - VirtualFileId, - VirtualFileVersion, - VirtualFileVersionDescription, - )>() - .await?; - - // Add mapping to local sheet - let hash = sha1_hash::calc_sha1(&full_path, 2048).await.unwrap().hash; - let time = std::fs::metadata(&full_path)?.modified()?; - local_sheet.add_mapping( - &path.clone(), - LocalMappingMetadata::new( - hash, // hash_when_updated - time, // time_when_updated - std::fs::metadata(&full_path)?.len(), // size_when_updated - version_desc, // version_desc_when_updated - version, // version_when_updated - vfid, // mapping_vfid - time, // last_modifiy_check_itme - false, // last_modifiy_check_result - ), - )?; - - // Print success info - if print_infos { - local_println!(local_output, "+ {}", path.display()); - } - - success_relative_pathes.push(path); - } - - // Write local sheet - local_sheet.write().await?; - - Ok(CreateTaskResult::Success(success_relative_pathes)) -} - -async fn proc_create_tasks_remote( - ctx: &ActionContext, - instance: Arc>, - member_id: &MemberId, - sheet_name: &SheetName, - relative_paths: Vec, -) -> Result { - let vault = try_get_vault(ctx)?; - let mut mut_instance = instance.lock().await; - - // Sheet check - let Ok(mut sheet) = vault.sheet(sheet_name).await else { - // Sheet not found - mut_instance.write_msgpack(false).await?; - return Ok(CreateTaskResult::SheetNotFound(sheet_name.to_string())); - }; - mut_instance.write_msgpack(true).await?; - - // Duplicate create precheck - for path in relative_paths.iter() { - if sheet.mapping().contains_key(path) { - // Duplicate file - mut_instance.write_msgpack((false, path)).await?; - return Ok(CreateTaskResult::CreateFileOnExistPath(path.clone())); - } - } - mut_instance.write_msgpack((true, PathBuf::new())).await?; - - let mut success_relative_pathes = Vec::new(); - - // Start receiving files - for path in relative_paths { - // Read file and create virtual file - let Ok(vfid) = vault - .create_virtual_file_from_connection(&mut mut_instance, member_id) - .await - else { - continue; - }; - - // Record virtual file to sheet - let vf_meta = vault.virtual_file(&vfid)?.read_meta().await?; - sheet - .add_mapping(path.clone(), vfid.clone(), vf_meta.version_latest()) - .await?; - - // Tell client the virtual file id and version - mut_instance - .write_msgpack(( - vfid, - vf_meta.version_latest(), - vf_meta - .version_description(vf_meta.version_latest()) - .unwrap(), - )) - .await?; - - success_relative_pathes.push(path); - } - - sheet.persist().await?; - - Ok(CreateTaskResult::Success(success_relative_pathes)) -} - -async fn proc_update_tasks_local( - ctx: &ActionContext, - instance: Arc>, - member_id: &MemberId, - sheet_name: &SheetName, - relative_paths: Vec, - print_infos: bool, - file_update_info: HashMap, -) -> Result { - let workspace = try_get_local_workspace(ctx)?; - let local_output = try_get_local_output(ctx)?; - let mut mut_instance = instance.lock().await; - let mut local_sheet = workspace.local_sheet(member_id, sheet_name).await?; - - let mut success = Vec::new(); - - if print_infos && relative_paths.len() > 0 { - local_println!(local_output, "Updating {} files...", relative_paths.len()); - } - - for path in relative_paths.iter() { - let Ok(mapping) = local_sheet.mapping_data(path) else { - // Is mapping not found, write empty - mut_instance.write_msgpack("".to_string()).await?; - continue; - }; - // Read and send file version - let Ok(_) = mut_instance - .write_msgpack(mapping.version_when_updated()) - .await - else { - continue; - }; - - // Read verify result - let verify_result: bool = mut_instance.read_msgpack().await?; - if !verify_result { - let reason = mut_instance.read_msgpack::().await?; - return Ok(UpdateTaskResult::VerifyFailed { - path: path.clone(), - reason: reason.clone(), - }); - } - - // Calc hash - let hash_result = match sha1_hash::calc_sha1(workspace.local_path().join(path), 2048).await - { - Ok(r) => r, - Err(_) => { - mut_instance.write_msgpack(false).await?; // Not Ready - continue; - } - }; - - // Get next version - let Some((next_version, description)) = file_update_info.get(path) else { - mut_instance.write_msgpack(false).await?; // Not Ready - continue; - }; - - // Write - mut_instance.write_msgpack(true).await?; // Ready - mut_instance.write_file(path).await?; - - // Read upload result - let upload_result: bool = mut_instance.read_msgpack().await?; - if upload_result { - // Success - let mapping_data_mut = local_sheet.mapping_data_mut(path).unwrap(); - let version = mapping_data_mut.version_when_updated().clone(); - mapping_data_mut.set_hash_when_updated(hash_result.hash); - mapping_data_mut.set_version_when_updated(next_version.clone()); - mapping_data_mut.set_version_desc_when_updated(VirtualFileVersionDescription { - creator: member_id.clone(), - description: description.clone(), - }); - mapping_data_mut.set_last_modifiy_check_result(false); // Mark file not modified - - // Write - local_sheet.write().await?; - - // Push path into success vec - success.push(path.clone()); - - // Print success info - if print_infos { - local_println!( - local_output, - "↑ {} ({} -> {})", - path.display(), - version, - next_version - ); - } - } - } - - Ok(UpdateTaskResult::Success(success)) -} - -async fn proc_update_tasks_remote( - ctx: &ActionContext, - instance: Arc>, - member_id: &MemberId, - sheet_name: &SheetName, - relative_paths: Vec, - file_update_info: HashMap, -) -> Result { - let vault = try_get_vault(ctx)?; - let mut mut_instance = instance.lock().await; - - let mut success = Vec::new(); - - for path in relative_paths.iter() { - // Read version - let Ok(version) = mut_instance.read_msgpack::().await else { - continue; - }; - if version.is_empty() { - continue; - } - - // Verify - let Some((next_version, description)) = file_update_info.get(path) else { - mut_instance.write_msgpack(false).await?; - let reason = VerifyFailReason::UpdateButNoDescription; - mut_instance.write_msgpack(reason.clone()).await?; - return Ok(UpdateTaskResult::VerifyFailed { - path: path.clone(), - reason, - }); // Sheet not found - }; - let Ok(mut sheet) = vault.sheet(sheet_name).await else { - mut_instance.write_msgpack(false).await?; - let reason = VerifyFailReason::SheetNotFound(sheet_name.clone()); - mut_instance.write_msgpack(reason.clone()).await?; - return Ok(UpdateTaskResult::VerifyFailed { - path: path.clone(), - reason, - }); // Sheet not found - }; - let Some(mapping_data) = sheet.mapping_mut().get_mut(path) else { - mut_instance.write_msgpack(false).await?; - let reason = VerifyFailReason::MappingNotFound; - mut_instance.write_msgpack(reason.clone()).await?; - return Ok(UpdateTaskResult::VerifyFailed { - path: path.clone(), - reason, - }); // Mapping not found - }; - let Ok(vf) = vault.virtual_file(&mapping_data.id) else { - mut_instance.write_msgpack(false).await?; - let reason = VerifyFailReason::VirtualFileNotFound(mapping_data.id.clone()); - mut_instance.write_msgpack(reason.clone()).await?; - return Ok(UpdateTaskResult::VerifyFailed { - path: path.clone(), - reason, - }); // Virtual file not found - }; - let Ok(vf_metadata) = vf.read_meta().await else { - mut_instance.write_msgpack(false).await?; - let reason = VerifyFailReason::VirtualFileReadFailed(mapping_data.id.clone()); - mut_instance.write_msgpack(reason.clone()).await?; - return Ok(UpdateTaskResult::VerifyFailed { - path: path.clone(), - reason, - }); // Read virtual file metadata failed - }; - if vf_metadata.versions().contains(next_version) { - mut_instance.write_msgpack(false).await?; - let reason = VerifyFailReason::VersionAlreadyExist(version); - mut_instance.write_msgpack(reason.clone()).await?; - return Ok(UpdateTaskResult::VerifyFailed { - path: path.clone(), - reason, - }); // VersionAlreadyExist - } - if vf_metadata.hold_member() != member_id { - mut_instance.write_msgpack(false).await?; - let reason = VerifyFailReason::NotHeld; - mut_instance.write_msgpack(reason.clone()).await?; - return Ok(UpdateTaskResult::VerifyFailed { - path: path.clone(), - reason, - }); // Member not held it - }; - if vf_metadata.version_latest() != version { - mut_instance.write_msgpack(false).await?; - let reason = - VerifyFailReason::VersionDismatch(version.clone(), vf_metadata.version_latest()); - mut_instance.write_msgpack(reason.clone()).await?; - return Ok(UpdateTaskResult::VerifyFailed { - path: path.clone(), - reason, - }); // Version does not match - }; - mut_instance.write_msgpack(true).await?; // Verified - - // Read if local ready - let ready: bool = mut_instance.read_msgpack().await?; - if !ready { - continue; - } - - // Read and update virtual file - match vault - .update_virtual_file_from_connection( - &mut mut_instance, - member_id, - &mapping_data.id, - next_version, - VirtualFileVersionDescription { - creator: member_id.clone(), - description: description.clone(), - }, - ) - .await - { - Ok(_) => { - // Update version to sheet - mapping_data.version = next_version.clone(); - - // Persist - sheet.persist().await?; - - success.push(path.clone()); - mut_instance.write_msgpack(true).await?; // Success - } - Err(e) => { - mut_instance.write_msgpack(false).await?; // Fail - return Err(e.into()); - } - } - } - - Ok(UpdateTaskResult::Success(success)) -} - -type SyncVersionInfo = Option<( - VirtualFileVersion, - VirtualFileVersionDescription, - VirtualFileId, -)>; - -async fn proc_sync_tasks_local( - ctx: &ActionContext, - instance: Arc>, - member_id: &MemberId, - sheet_name: &SheetName, - relative_paths: Vec, - print_infos: bool, -) -> Result { - let workspace = try_get_local_workspace(ctx)?; - let local_output = try_get_local_output(ctx)?; - let mut mut_instance = instance.lock().await; - let mut success: Vec = Vec::new(); - - if print_infos && relative_paths.len() > 0 { - local_println!(local_output, "Syncing {} files...", relative_paths.len()); - } - - for path in relative_paths { - let Some((version, description, vfid)) = - mut_instance.read_msgpack::().await? - else { - continue; - }; - - // Generate a temp path - let temp_path = workspace - .local_path() - .join(CLIENT_FILE_TEMP_FILE.replace(KEY_TEMP_NAME, &VaultUuid::new_v4().to_string())); - - let copy_to = workspace.local_path().join(&path); - - // Read file - match mut_instance.read_file(&temp_path).await { - Ok(_) => { - if !temp_path.exists() { - continue; - } - } - Err(_) => { - continue; - } - } - - // Calc hash - let new_hash = match calc_sha1(&temp_path, 2048).await { - Ok(hash) => hash, - Err(_) => { - continue; - } - }; - - // Calc size - let new_size = match fs::metadata(&temp_path).await.map(|meta| meta.len()) { - Ok(size) => size, - Err(_) => { - continue; - } - }; - - // Write file - if copy_to.exists() { - if let Err(_) = fs::remove_file(©_to).await { - continue; - } - } else { - // Not exist, create directory - if let Some(path) = copy_to.clone().parent() { - fs::create_dir_all(path).await?; - } - } - if let Err(_) = fs::rename(&temp_path, ©_to).await { - continue; - } - - // Modify local sheet - let mut local_sheet = match workspace.local_sheet(member_id, sheet_name).await { - Ok(sheet) => sheet, - Err(_) => { - continue; - } - }; - - // Get or create mapping - let mapping = match local_sheet.mapping_data_mut(&path) { - Ok(m) => m, - Err(_) => { - // First download - let mut data = LocalMappingMetadata::default(); - data.set_mapping_vfid(vfid); - if let Err(_) = local_sheet.add_mapping(&path, data) { - continue; - } - match local_sheet.mapping_data_mut(&path) { - Ok(m) => m, - Err(_) => { - continue; - } - } - } - }; - - let time = SystemTime::now(); - mapping.set_hash_when_updated(new_hash.hash); - mapping.set_last_modifiy_check_result(false); // Mark not modified - mapping.set_version_when_updated(version); - mapping.set_version_desc_when_updated(description); - mapping.set_size_when_updated(new_size); - mapping.set_time_when_updated(time); - mapping.set_last_modifiy_check_time(time); - if let Err(_) = local_sheet.write().await { - continue; - } - - success.push(path.clone()); - - // Print success info - if print_infos { - local_println!(local_output, "↓ {}", path.display()); - } - } - Ok(SyncTaskResult::Success(success)) -} - -async fn proc_sync_tasks_remote( - ctx: &ActionContext, - instance: Arc>, - _member_id: &MemberId, - sheet_name: &SheetName, - relative_paths: Vec, -) -> Result { - let vault = try_get_vault(ctx)?; - let sheet = vault.sheet(sheet_name).await?; - let mut mut_instance = instance.lock().await; - let mut success: Vec = Vec::new(); - - for path in relative_paths { - // Get mapping - let Some(mapping) = sheet.mapping().get(&path) else { - mut_instance.write_msgpack::(None).await?; // (ready) - continue; - }; - // Get virtual file - let Ok(vf) = vault.virtual_file(&mapping.id) else { - mut_instance.write_msgpack::(None).await?; // (ready) - continue; - }; - // Read metadata and get real path - let vf_meta = &vf.read_meta().await?; - let real_path = vault.virtual_file_real_path(&mapping.id, &vf_meta.version_latest()); - let version = vf_meta.version_latest(); - mut_instance - .write_msgpack::(Some(( - version.clone(), - vf_meta.version_description(version).cloned().unwrap_or( - VirtualFileVersionDescription { - creator: MemberId::default(), - description: "".to_string(), - }, - ), - vf.id(), - ))) - .await?; // (ready) - if mut_instance.write_file(real_path).await.is_err() { - continue; - } else { - success.push(path); - } - } - Ok(SyncTaskResult::Success(success)) -} diff --git a/actions/src/remote_actions/edit_right_manage.rs b/actions/src/remote_actions/edit_right_manage.rs deleted file mode 100644 index 52305f5..0000000 --- a/actions/src/remote_actions/edit_right_manage.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod change_virtual_file_edit_right; diff --git a/actions/src/remote_actions/edit_right_manage/change_virtual_file_edit_right.rs b/actions/src/remote_actions/edit_right_manage/change_virtual_file_edit_right.rs deleted file mode 100644 index 1045bce..0000000 --- a/actions/src/remote_actions/edit_right_manage/change_virtual_file_edit_right.rs +++ /dev/null @@ -1,144 +0,0 @@ -use std::path::PathBuf; - -use action_system::{action::ActionContext, macros::action_gen}; -use serde::{Deserialize, Serialize}; -use tcp_connection::error::TcpTargetError; -use vcs_data::data::local::modified_status::sign_vault_modified; - -use crate::remote_actions::{ - auth_member, check_connection_instance, get_current_sheet_name, try_get_vault, -}; - -#[derive(Serialize, Deserialize)] -pub enum ChangeVirtualFileEditRightResult { - // Success - Success { - success_hold: Vec, - success_throw: Vec, - }, - - // Fail - AuthorizeFailed(String), - DoNothing, -} - -#[derive(Serialize, Deserialize, PartialEq, Clone)] -pub enum EditRightChangeBehaviour { - Hold, - Throw, -} - -/// The server part only checks: -/// 1. Whether the file exists -/// 2. Whether the file has no holder -/// If both conditions are met, send success information to the local client -/// -/// All version checks are handled locally -#[action_gen] -pub async fn change_virtual_file_edit_right_action( - ctx: ActionContext, - arguments: (Vec<(PathBuf, EditRightChangeBehaviour)>, bool), -) -> Result { - let instance = check_connection_instance(&ctx)?; - let (relative_paths, print_info) = arguments; - - // Auth Member - let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { - Ok(id) => id, - Err(e) => { - return Ok(ChangeVirtualFileEditRightResult::AuthorizeFailed( - e.to_string(), - )); - } - }; - - // Check sheet - let (sheet_name, _is_ref_sheet) = - get_current_sheet_name(&ctx, instance, &member_id, true).await?; - - if ctx.is_proc_on_remote() { - let mut mut_instance = instance.lock().await; - let mut success_hold: Vec = Vec::new(); - let mut success_throw: Vec = Vec::new(); - let vault = try_get_vault(&ctx)?; - for (path, behaviour) in relative_paths { - let Ok(sheet) = vault.sheet(&sheet_name).await else { - continue; - }; - let Some(mapping) = sheet.mapping().get(&path) else { - continue; - }; - let Ok(has_edit_right) = vault - .has_virtual_file_edit_right(&member_id, &mapping.id) - .await - else { - continue; - }; - - // Hold file - if !has_edit_right && behaviour == EditRightChangeBehaviour::Hold { - match vault - .grant_virtual_file_edit_right(&member_id, &mapping.id) - .await - { - Ok(_) => { - success_hold.push(path.clone()); - } - Err(_) => continue, - } - } else - // Throw file - if (has_edit_right || is_host_mode) - && behaviour == EditRightChangeBehaviour::Throw - { - match vault.revoke_virtual_file_edit_right(&mapping.id).await { - Ok(_) => { - success_throw.push(path.clone()); - } - Err(_) => continue, - } - } - } - - // Write success list - mut_instance - .write_large_msgpack::<(Vec, Vec)>( - (success_hold.clone(), success_throw.clone()), - 4096u16, - ) - .await?; - return Ok(ChangeVirtualFileEditRightResult::Success { - success_hold, - success_throw, - }); - } - - if ctx.is_proc_on_local() { - let mut mut_instance = instance.lock().await; - let (success_hold, success_throw) = mut_instance - .read_large_msgpack::<(Vec, Vec)>(4096u16) - .await?; - - // If there are any successful items, mark as modified - if success_hold.len() + success_throw.len() > 0 { - sign_vault_modified(true).await; - } - - // Print info - if print_info { - success_hold - .iter() - .for_each(|s| println!("--> {}", s.display())); - success_throw - .iter() - .for_each(|s| println!("<-- {}", s.display())); - } - - return Ok(ChangeVirtualFileEditRightResult::Success { - success_hold, - success_throw, - }); - } - - Ok(ChangeVirtualFileEditRightResult::DoNothing) -} diff --git a/actions/src/remote_actions/mapping_manage.rs b/actions/src/remote_actions/mapping_manage.rs deleted file mode 100644 index 624b4ab..0000000 --- a/actions/src/remote_actions/mapping_manage.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod edit_mapping; -pub mod merge_share_mapping; -pub mod share_mapping; diff --git a/actions/src/remote_actions/mapping_manage/edit_mapping.rs b/actions/src/remote_actions/mapping_manage/edit_mapping.rs deleted file mode 100644 index 3c39c5d..0000000 --- a/actions/src/remote_actions/mapping_manage/edit_mapping.rs +++ /dev/null @@ -1,157 +0,0 @@ -use std::collections::HashMap; - -use action_system::{action::ActionContext, macros::action_gen}; -use serde::{Deserialize, Serialize}; -use tcp_connection::error::TcpTargetError; -use vcs_data::data::local::{ - modified_status::sign_vault_modified, - workspace_analyzer::{FromRelativePathBuf, ToRelativePathBuf}, -}; - -use crate::{ - remote_actions::{ - auth_member, check_connection_instance, get_current_sheet_name, try_get_vault, - }, - write_and_return, -}; - -pub type OperationArgument = (EditMappingOperations, Option); - -#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)] -pub enum EditMappingOperations { - Move, - Erase, -} - -#[derive(Serialize, Deserialize, Default)] -pub enum EditMappingActionResult { - Success, - - // Fail - AuthorizeFailed(String), - EditNotAllowed, - MappingNotFound(FromRelativePathBuf), - InvalidMove(InvalidMoveReason), - - #[default] - Unknown, -} - -#[derive(Serialize, Deserialize)] -pub enum InvalidMoveReason { - MoveOperationButNoTarget(FromRelativePathBuf), - ContainsDuplicateMapping(ToRelativePathBuf), -} - -#[derive(Serialize, Deserialize, Clone)] -pub struct EditMappingActionArguments { - pub operations: HashMap, -} - -/// This Action only modifies Sheet Mapping and -/// does not interfere with the actual location of local files or Local Mapping -#[action_gen] -pub async fn edit_mapping_action( - ctx: ActionContext, - args: EditMappingActionArguments, -) -> Result { - let instance = check_connection_instance(&ctx)?; - - // Auth Member - let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { - Ok(id) => id, - Err(e) => { - return Ok(EditMappingActionResult::AuthorizeFailed(e.to_string())); - } - }; - - // Check sheet - let (sheet_name, is_ref_sheet) = - get_current_sheet_name(&ctx, instance, &member_id, true).await?; - - // Can modify Sheet when not in reference sheet or in Host mode - let can_modify_sheet = !is_ref_sheet || is_host_mode; - - if !can_modify_sheet { - return Ok(EditMappingActionResult::EditNotAllowed); - } - - if ctx.is_proc_on_remote() { - let vault = try_get_vault(&ctx)?; - let mut sheet = vault.sheet(&sheet_name).await?; - - // Precheck - for (from_path, (operation, to_path)) in args.operations.iter() { - // Check mapping exists - if !sheet.mapping().contains_key(from_path) { - write_and_return!( - instance, - EditMappingActionResult::MappingNotFound(from_path.clone()) - ); - } - - // Move check - if operation == &EditMappingOperations::Move { - // Check if target exists - if let Some(to_path) = to_path { - // Check if target is duplicate - if sheet.mapping().contains_key(to_path) { - write_and_return!( - instance, - EditMappingActionResult::InvalidMove( - InvalidMoveReason::ContainsDuplicateMapping(to_path.clone()) - ) - ); - } - } else { - write_and_return!( - instance, - EditMappingActionResult::InvalidMove( - InvalidMoveReason::MoveOperationButNoTarget(from_path.clone()) - ) - ); - } - } - } - - // Process - for (from_path, (operation, to_path)) in args.operations { - match operation { - // During the Precheck phase, it has been ensured that: - // 1. The mapping to be edited for the From path indeed exists - // 2. The location of the To path is indeed empty - // 3. In Move mode, To path can be safely unwrapped - // Therefore, the following unwrap() calls are safe to execute - EditMappingOperations::Move => { - let mapping = sheet.mapping_mut().remove(&from_path).unwrap(); - let to_path = to_path.unwrap(); - sheet - .add_mapping(to_path, mapping.id, mapping.version) - .await?; - } - EditMappingOperations::Erase => { - sheet.mapping_mut().remove(&from_path).unwrap(); - } - } - } - - // Write - sheet.persist().await?; - - write_and_return!(instance, EditMappingActionResult::Success); - } - - if ctx.is_proc_on_local() { - let result = instance - .lock() - .await - .read::() - .await?; - if matches!(result, EditMappingActionResult::Success) { - sign_vault_modified(true).await; - } - return Ok(result); - } - - Ok(EditMappingActionResult::Success) -} diff --git a/actions/src/remote_actions/mapping_manage/merge_share_mapping.rs b/actions/src/remote_actions/mapping_manage/merge_share_mapping.rs deleted file mode 100644 index df889a1..0000000 --- a/actions/src/remote_actions/mapping_manage/merge_share_mapping.rs +++ /dev/null @@ -1,117 +0,0 @@ -use std::io::ErrorKind; - -use action_system::{action::ActionContext, macros::action_gen}; -use serde::{Deserialize, Serialize}; -use tcp_connection::error::TcpTargetError; -use vcs_data::data::{ - local::modified_status::sign_vault_modified, - vault::mapping_share::{ShareMergeMode, SheetShareId}, -}; - -use crate::{ - remote_actions::{ - auth_member, check_connection_instance, get_current_sheet_name, try_get_vault, - }, - write_and_return, -}; - -#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)] -pub struct MergeShareMappingArguments { - pub share_id: SheetShareId, - pub share_merge_mode: ShareMergeMode, -} - -#[derive(Serialize, Deserialize, Default)] -pub enum MergeShareMappingActionResult { - Success, - - // Fail - HasConflicts, - AuthorizeFailed(String), - EditNotAllowed, - ShareIdNotFound(SheetShareId), - MergeFails(String), - - #[default] - Unknown, -} - -#[action_gen] -pub async fn merge_share_mapping_action( - ctx: ActionContext, - args: MergeShareMappingArguments, -) -> Result { - let instance = check_connection_instance(&ctx)?; - - // Auth Member - let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { - Ok(id) => id, - Err(e) => { - return Ok(MergeShareMappingActionResult::AuthorizeFailed( - e.to_string(), - )); - } - }; - - // Check sheet - let (sheet_name, is_ref_sheet) = - get_current_sheet_name(&ctx, instance, &member_id, true).await?; - - // Can modify Sheet when not in reference sheet or in Host mode - let can_modify_sheet = !is_ref_sheet || is_host_mode; - - if !can_modify_sheet { - return Ok(MergeShareMappingActionResult::EditNotAllowed); - } - - if ctx.is_proc_on_remote() { - let vault = try_get_vault(&ctx)?; - let share_id = args.share_id; - - // Get the share and sheet - let (sheet, share) = if vault.share_file_path(&sheet_name, &share_id).exists() { - let sheet = vault.sheet(&sheet_name).await?; - let share = sheet.get_share(&share_id).await?; - (sheet, share) - } else { - // Share does not exist - write_and_return!( - instance, - MergeShareMappingActionResult::ShareIdNotFound(share_id.clone()) - ); - }; - - // Perform the merge - match sheet.merge_share(share, args.share_merge_mode).await { - Ok(_) => write_and_return!(instance, MergeShareMappingActionResult::Success), - Err(e) => match e.kind() { - ErrorKind::AlreadyExists => { - write_and_return!(instance, MergeShareMappingActionResult::HasConflicts); - } - _ => { - write_and_return!( - instance, - MergeShareMappingActionResult::MergeFails(e.to_string()) - ); - } - }, - } - } - - if ctx.is_proc_on_local() { - let result = instance - .lock() - .await - .read::() - .await?; - match result { - MergeShareMappingActionResult::Success => { - sign_vault_modified(true).await; - } - _ => {} - } - return Ok(result); - } - - Ok(MergeShareMappingActionResult::Success) -} diff --git a/actions/src/remote_actions/mapping_manage/share_mapping.rs b/actions/src/remote_actions/mapping_manage/share_mapping.rs deleted file mode 100644 index 5c77e53..0000000 --- a/actions/src/remote_actions/mapping_manage/share_mapping.rs +++ /dev/null @@ -1,135 +0,0 @@ -use action_system::{action::ActionContext, macros::action_gen}; -use serde::{Deserialize, Serialize}; -use tcp_connection::error::TcpTargetError; -use vcs_data::{ - constants::VAULT_HOST_NAME, - data::{local::workspace_analyzer::FromRelativePathBuf, sheet::SheetName}, -}; - -use crate::{ - remote_actions::{ - auth_member, check_connection_instance, get_current_sheet_name, try_get_vault, - }, - write_and_return, -}; - -#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)] -pub struct ShareMappingArguments { - pub mappings: Vec, - pub description: String, - // None = current sheet, - // Some(sheet_name) = other ref(public) sheet - pub from_sheet: Option, - pub to_sheet: SheetName, -} - -#[derive(Serialize, Deserialize, Default)] -pub enum ShareMappingActionResult { - Success, - - // Fail - AuthorizeFailed(String), - TargetSheetNotFound(SheetName), - TargetIsSelf, - MappingNotFound(FromRelativePathBuf), - - #[default] - Unknown, -} - -#[action_gen] -pub async fn share_mapping_action( - ctx: ActionContext, - args: ShareMappingArguments, -) -> Result { - let instance = check_connection_instance(&ctx)?; - - // Auth Member - let (member_id, _is_host_mode) = match auth_member(&ctx, instance).await { - Ok(id) => id, - Err(e) => { - return Ok(ShareMappingActionResult::AuthorizeFailed(e.to_string())); - } - }; - - // Check sheet - let sheet_name = args.from_sheet.unwrap_or( - get_current_sheet_name(&ctx, instance, &member_id, true) - .await? - .0, - ); - - if ctx.is_proc_on_remote() { - let vault = try_get_vault(&ctx)?; - let sheet = vault.sheet(&sheet_name).await?; - - // Tip: Because sheet_name may specify a sheet that does not belong to the user, - // a secondary verification is required. - - // Check if the sheet holder is Some and matches the member_id or is the host - let Some(holder) = sheet.holder() else { - // If there's no holder, the sheet cannot be shared from - write_and_return!( - instance, - ShareMappingActionResult::AuthorizeFailed("Sheet has no holder".to_string()) - ); - }; - - // Verify the holder is either the current member or the host - if holder != &member_id && holder != VAULT_HOST_NAME { - write_and_return!( - instance, - ShareMappingActionResult::AuthorizeFailed( - "Not sheet holder or ref sheet".to_string() - ) - ); - } - - let to_sheet_name = args.to_sheet; - - // Verify target sheet exists - if !vault.sheet_names()?.contains(&to_sheet_name) { - // Does not exist - write_and_return!( - instance, - ShareMappingActionResult::TargetSheetNotFound(to_sheet_name.clone()) - ); - } - - // Verify sheet is not self - if sheet_name == to_sheet_name { - // Is self - write_and_return!(instance, ShareMappingActionResult::TargetIsSelf); - } - - // Verify all mappings are correct - for mapping in args.mappings.iter() { - if !sheet.mapping().contains_key(mapping) { - // If any mapping is invalid, indicate failure - write_and_return!( - instance, - ShareMappingActionResult::MappingNotFound(mapping.clone()) - ); - } - } - - // Execute sharing logic - sheet - .share_mappings(&to_sheet_name, args.mappings, &member_id, args.description) - .await?; - - // Sharing successful - write_and_return!(instance, ShareMappingActionResult::Success); - } - - if ctx.is_proc_on_local() { - let result = instance - .lock() - .await - .read::() - .await?; - return Ok(result); - } - - Ok(ShareMappingActionResult::Success) -} diff --git a/actions/src/remote_actions/sheet_manage.rs b/actions/src/remote_actions/sheet_manage.rs deleted file mode 100644 index a3a91a2..0000000 --- a/actions/src/remote_actions/sheet_manage.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod drop_sheet; -pub mod make_sheet; diff --git a/actions/src/remote_actions/sheet_manage/drop_sheet.rs b/actions/src/remote_actions/sheet_manage/drop_sheet.rs deleted file mode 100644 index e21f3dd..0000000 --- a/actions/src/remote_actions/sheet_manage/drop_sheet.rs +++ /dev/null @@ -1,123 +0,0 @@ -use std::io::ErrorKind; - -use action_system::{action::ActionContext, macros::action_gen}; -use serde::{Deserialize, Serialize}; -use tcp_connection::error::TcpTargetError; -use vcs_data::data::{local::modified_status::sign_vault_modified, sheet::SheetName}; - -use crate::{ - remote_actions::{ - auth_member, check_connection_instance, try_get_local_workspace, try_get_vault, - }, - write_and_return, -}; - -#[derive(Default, Serialize, Deserialize)] -pub enum DropSheetActionResult { - Success, - - // Fail - SheetInUse, - AuthorizeFailed(String), - SheetNotExists, - SheetDropFailed(String), - NoHolder, - NotOwner, - - #[default] - Unknown, -} - -#[action_gen] -pub async fn drop_sheet_action( - ctx: ActionContext, - sheet_name: SheetName, -) -> Result { - let instance = check_connection_instance(&ctx)?; - - // Auth Member - let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { - Ok(id) => id, - Err(e) => { - return Ok(DropSheetActionResult::AuthorizeFailed(e.to_string())); - } - }; - - // Check sheet in use on local - if ctx.is_proc_on_local() { - let local_workspace = try_get_local_workspace(&ctx)?; - if let Some(sheet) = local_workspace.config().lock().await.sheet_in_use() { - if sheet == &sheet_name { - instance.lock().await.write(false).await?; - return Ok(DropSheetActionResult::SheetInUse); - } - instance.lock().await.write(true).await?; - } else { - instance.lock().await.write(true).await?; - } - } - - if ctx.is_proc_on_remote() { - // Check if client sheet is in use - let sheet_in_use = instance.lock().await.read::().await?; - if !sheet_in_use { - return Ok(DropSheetActionResult::SheetInUse); - } - - let vault = try_get_vault(&ctx)?; - - // Check if the sheet exists - let mut sheet = match vault.sheet(&sheet_name).await { - Ok(sheet) => sheet, - Err(e) => { - if e.kind() == ErrorKind::NotFound { - write_and_return!(instance, DropSheetActionResult::SheetNotExists); - } else { - write_and_return!( - instance, - DropSheetActionResult::SheetDropFailed(e.to_string()) - ); - } - } - }; - - // Get the sheet's holder - let Some(holder) = sheet.holder() else { - write_and_return!(instance, DropSheetActionResult::NoHolder); - }; - - // Verify that the sheet holder is either the current user or the host - // All sheets belong to the host - if holder != &member_id && !is_host_mode { - write_and_return!(instance, DropSheetActionResult::NotOwner); - } - - // Drop the sheet - sheet.forget_holder(); - match sheet.persist().await { - Ok(_) => { - write_and_return!(instance, DropSheetActionResult::Success); - } - Err(e) => { - write_and_return!( - instance, - DropSheetActionResult::SheetDropFailed(e.to_string()) - ); - } - } - } - - if ctx.is_proc_on_local() { - let result = instance - .lock() - .await - .read::() - .await?; - if matches!(result, DropSheetActionResult::Success) { - sign_vault_modified(true).await; - } - return Ok(result); - } - - Err(TcpTargetError::NoResult("No result.".to_string())) -} diff --git a/actions/src/remote_actions/sheet_manage/make_sheet.rs b/actions/src/remote_actions/sheet_manage/make_sheet.rs deleted file mode 100644 index a323413..0000000 --- a/actions/src/remote_actions/sheet_manage/make_sheet.rs +++ /dev/null @@ -1,98 +0,0 @@ -use action_system::{action::ActionContext, macros::action_gen}; -use serde::{Deserialize, Serialize}; -use tcp_connection::error::TcpTargetError; -use vcs_data::{ - constants::VAULT_HOST_NAME, - data::{local::modified_status::sign_vault_modified, sheet::SheetName}, -}; - -use crate::{ - remote_actions::{auth_member, check_connection_instance, try_get_vault}, - write_and_return, -}; - -#[derive(Default, Serialize, Deserialize)] -pub enum MakeSheetActionResult { - Success, - SuccessRestore, - - // Fail - AuthorizeFailed(String), - SheetAlreadyExists, - SheetCreationFailed(String), - - #[default] - Unknown, -} - -/// Build a sheet with context -#[action_gen] -pub async fn make_sheet_action( - ctx: ActionContext, - sheet_name: SheetName, -) -> Result { - let instance = check_connection_instance(&ctx)?; - - // Auth Member - let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { - Ok(id) => id, - Err(e) => return Ok(MakeSheetActionResult::AuthorizeFailed(e.to_string())), - }; - - if ctx.is_proc_on_remote() { - let vault = try_get_vault(&ctx)?; - let holder = if is_host_mode { - VAULT_HOST_NAME.to_string() - } else { - member_id - }; - - // Check if the sheet already exists - if let Ok(mut sheet) = vault.sheet(&sheet_name).await { - // If the sheet has no holder, assign it to the current member (restore operation) - if sheet.holder().is_none() { - sheet.set_holder(holder.clone()); - match sheet.persist().await { - Ok(_) => { - write_and_return!(instance, MakeSheetActionResult::SuccessRestore); - } - Err(e) => { - write_and_return!( - instance, - MakeSheetActionResult::SheetCreationFailed(e.to_string()) - ); - } - } - } else { - write_and_return!(instance, MakeSheetActionResult::SheetAlreadyExists); - } - } else { - // Create the sheet - match vault.create_sheet(&sheet_name, &holder).await { - Ok(_) => { - write_and_return!(instance, MakeSheetActionResult::Success); - } - Err(e) => { - write_and_return!( - instance, - MakeSheetActionResult::SheetCreationFailed(e.to_string()) - ); - } - } - } - } - - if ctx.is_proc_on_local() { - let result = instance - .lock() - .await - .read::() - .await?; - if matches!(result, MakeSheetActionResult::Success) { - sign_vault_modified(true).await; - } - return Ok(result); - } - - Err(TcpTargetError::NoResult("No result.".to_string())) -} diff --git a/actions/src/remote_actions/workspace_manage.rs b/actions/src/remote_actions/workspace_manage.rs deleted file mode 100644 index 15f70e8..0000000 --- a/actions/src/remote_actions/workspace_manage.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod set_upstream_vault; -pub mod update_to_latest_info; diff --git a/actions/src/remote_actions/workspace_manage/set_upstream_vault.rs b/actions/src/remote_actions/workspace_manage/set_upstream_vault.rs deleted file mode 100644 index ba45214..0000000 --- a/actions/src/remote_actions/workspace_manage/set_upstream_vault.rs +++ /dev/null @@ -1,101 +0,0 @@ -use std::net::SocketAddr; - -use action_system::{action::ActionContext, macros::action_gen}; -use cfg_file::config::ConfigFile; -use log::info; -use serde::{Deserialize, Serialize}; -use tcp_connection::error::TcpTargetError; -use vcs_data::data::{local::workspace_config::LocalConfig, vault::vault_config::VaultUuid}; - -use crate::remote_actions::{ - auth_member, check_connection_instance, try_get_local_workspace, try_get_vault, -}; - -#[derive(Serialize, Deserialize)] -pub enum SetUpstreamVaultActionResult { - // Success - DirectedAndStained, - Redirected, - - // Fail - AlreadyStained, - AuthorizeFailed(String), - RedirectFailed(String), - SameUpstream, - - Done, -} - -#[action_gen] -pub async fn set_upstream_vault_action( - ctx: ActionContext, - upstream: SocketAddr, -) -> Result { - let instance = check_connection_instance(&ctx)?; - - // Auth Member - if let Err(e) = auth_member(&ctx, instance).await { - return Ok(SetUpstreamVaultActionResult::AuthorizeFailed(e.to_string())); - } - - // Direct - if ctx.is_proc_on_remote() { - let vault = try_get_vault(&ctx)?; - instance - .lock() - .await - .write(*vault.config().vault_uuid()) - .await?; - return Ok(SetUpstreamVaultActionResult::Done); - } - - if ctx.is_proc_on_local() { - info!("Authorize successful. directing to upstream vault."); - - // Read the vault UUID from the instance - let vault_uuid = instance.lock().await.read::().await?; - - let local_workspace = try_get_local_workspace(&ctx)?; - let local_config = local_workspace.config(); - - let mut mut_local_config = local_config.lock().await; - if !mut_local_config.stained() { - // Stain the local workspace - mut_local_config.stain(vault_uuid); - - // Set the upstream address - mut_local_config.set_vault_addr(upstream); - - // Store the updated config - LocalConfig::write(&mut_local_config).await?; - - info!("Workspace stained!"); - return Ok(SetUpstreamVaultActionResult::DirectedAndStained); - } else { - // Local workspace is already stained, redirecting - let Some(stained_uuid) = mut_local_config.stained_uuid() else { - return Ok(SetUpstreamVaultActionResult::RedirectFailed( - "Stained uuid not found".to_string(), - )); - }; - let local_upstream = mut_local_config.upstream_addr(); - - // Address changed, but same UUID. - if vault_uuid == stained_uuid { - if local_upstream != upstream { - // Set the upstream address - mut_local_config.set_vault_addr(upstream); - - // Store the updated config - LocalConfig::write(&mut_local_config).await?; - return Ok(SetUpstreamVaultActionResult::Redirected); - } else { - return Ok(SetUpstreamVaultActionResult::SameUpstream); - } - } - return Ok(SetUpstreamVaultActionResult::AlreadyStained); - } - } - - Err(TcpTargetError::NoResult("No result.".to_string())) -} diff --git a/actions/src/remote_actions/workspace_manage/update_to_latest_info.rs b/actions/src/remote_actions/workspace_manage/update_to_latest_info.rs deleted file mode 100644 index cd17c32..0000000 --- a/actions/src/remote_actions/workspace_manage/update_to_latest_info.rs +++ /dev/null @@ -1,433 +0,0 @@ -use std::{ - collections::{HashMap, HashSet}, - io::ErrorKind, - path::PathBuf, - time::SystemTime, -}; - -use action_system::{action::ActionContext, macros::action_gen}; -use cfg_file::config::ConfigFile; -use log::info; -use serde::{Deserialize, Serialize}; -use tcp_connection::error::TcpTargetError; -use vcs_data::{ - constants::{ - CLIENT_PATH_CACHED_SHEET, CLIENT_PATH_LOCAL_SHEET, REF_SHEET_NAME, - SERVER_SUFFIX_SHEET_SHARE_FILE, VAULT_HOST_NAME, - }, - data::{ - local::{ - cached_sheet::CachedSheet, - latest_file_data::LatestFileData, - latest_info::{LatestInfo, SheetInfo}, - modified_status::sign_vault_modified, - }, - member::MemberId, - sheet::{SheetData, SheetName, SheetPathBuf}, - vault::{ - mapping_share::{Share, SheetShareId}, - virtual_file::{VirtualFileId, VirtualFileVersion, VirtualFileVersionDescription}, - }, - }, -}; - -use crate::remote_actions::{ - auth_member, check_connection_instance, try_get_local_workspace, try_get_vault, -}; - -#[derive(Serialize, Deserialize)] -pub enum UpdateToLatestInfoResult { - Success, - - // Fail - AuthorizeFailed(String), - SyncCachedSheetFail(SyncCachedSheetFailReason), -} - -#[derive(Serialize, Deserialize)] -pub enum SyncCachedSheetFailReason { - PathAlreadyExist(PathBuf), -} - -#[action_gen] -pub async fn update_to_latest_info_action( - ctx: ActionContext, - _unused: (), -) -> Result { - let instance = check_connection_instance(&ctx)?; - - let (member_id, _is_host_mode) = match auth_member(&ctx, instance).await { - Ok(id) => id, - Err(e) => return Ok(UpdateToLatestInfoResult::AuthorizeFailed(e.to_string())), - }; - - info!("Sending latest info to {}", member_id); - - // Sync Latest Info - { - if ctx.is_proc_on_remote() { - let vault = try_get_vault(&ctx)?; - - // Build latest info - let mut latest_info = LatestInfo::default(); - - // Sheet & Share - let mut shares_in_my_sheets: HashMap> = - HashMap::new(); - let mut member_owned = Vec::new(); - let mut member_visible = Vec::new(); - let mut ref_sheets = HashSet::new(); - - for sheet in vault.sheets().await? { - // Build share parts - if let Some(holder) = sheet.holder() { - if holder == &member_id || holder == VAULT_HOST_NAME { - let mut sheet_shares: HashMap = HashMap::new(); - for share in sheet.get_shares().await? { - // Get SharePath - let Some(share_path) = share.path.clone() else { - continue; - }; - // Get ShareId from SharePath - let Some(share_id) = share_path.file_name() else { - continue; - }; - let share_id = share_id.display().to_string(); - let share_id_trimed = - share_id.trim_end_matches(SERVER_SUFFIX_SHEET_SHARE_FILE); - sheet_shares.insert(share_id_trimed.to_string(), share); - } - shares_in_my_sheets.insert(sheet.name().clone(), sheet_shares); - } - } - - // Build sheet parts - let holder_is_host = - sheet.holder().unwrap_or(&String::default()) == &VAULT_HOST_NAME; - if sheet.holder().is_some() - && (sheet.holder().unwrap() == &member_id || holder_is_host) - { - member_owned.push(sheet.name().clone()); - if holder_is_host { - ref_sheets.insert(sheet.name().clone()); - } - } else { - member_visible.push(SheetInfo { - sheet_name: sheet.name().clone(), - holder_name: sheet.holder().cloned(), - }); - } - } - - // Record Share & Sheet - latest_info.visible_sheets = member_owned; - latest_info.invisible_sheets = member_visible; - latest_info.shares_in_my_sheets = shares_in_my_sheets; - - // RefSheet - let ref_sheet_data = vault.sheet(&REF_SHEET_NAME.to_string()).await?.to_data(); - latest_info.ref_sheet_content = ref_sheet_data.clone(); - latest_info.ref_sheet_vfs_mapping = ref_sheet_data - .mapping() - .into_iter() - .map(|(path, file)| (file.id.clone(), path.clone())) - .collect::>(); - latest_info.reference_sheets = ref_sheets; - - // Members - let members = vault.members().await?; - latest_info.vault_members = members; - - // Send - instance - .lock() - .await - .write_large_msgpack(latest_info, 512_u16) - .await?; - } - - if ctx.is_proc_on_local() { - let workspace = try_get_local_workspace(&ctx)?; - let mut latest_info = instance - .lock() - .await - .read_large_msgpack::(512_u16) - .await?; - latest_info.update_instant = Some(SystemTime::now()); - LatestInfo::write_to( - &latest_info, - LatestInfo::latest_info_path(workspace.local_path(), &member_id), - ) - .await?; - } - } - - info!("Update sheets to {}", member_id); - - // Sync Remote Sheets - { - if ctx.is_proc_on_local() { - let workspace = try_get_local_workspace(&ctx)?; - let Ok(latest_info) = LatestInfo::read_from(LatestInfo::latest_info_path( - workspace.local_path(), - &member_id, - )) - .await - else { - return Err(TcpTargetError::Io("Read latest info failed".to_string())); - }; - - // Collect all local versions - let mut local_versions = vec![]; - for request_sheet in latest_info.visible_sheets { - let Ok(data) = CachedSheet::cached_sheet_data(&request_sheet).await else { - // For newly created sheets, the version is 0. - // Send -1 to distinguish from 0, ensuring the upstream will definitely send the sheet information - local_versions.push((request_sheet, -1)); - continue; - }; - local_versions.push((request_sheet, data.write_count())); - } - - // Send the version list - let len = local_versions.len(); - instance.lock().await.write_msgpack(local_versions).await?; - - if len < 1 { - // Don't return here, continue to next section - // But we need to consume the false marker from the server - if ctx.is_proc_on_local() { - let mut mut_instance = instance.lock().await; - let _: bool = mut_instance.read_msgpack().await?; - } - } else { - // Receive data - if ctx.is_proc_on_local() { - let mut mut_instance = instance.lock().await; - loop { - let in_coming: bool = mut_instance.read_msgpack().await?; - if in_coming { - let (sheet_name, data): (SheetName, SheetData) = - mut_instance.read_large_msgpack(1024u16).await?; - - let Some(path) = CachedSheet::cached_sheet_path(sheet_name) else { - return Err(TcpTargetError::NotFound( - "Workspace not found".to_string(), - )); - }; - - SheetData::write_to(&data, path).await?; - } else { - break; - } - } - } - } - } - if ctx.is_proc_on_remote() { - let vault = try_get_vault(&ctx)?; - let mut mut_instance = instance.lock().await; - - let local_versions = mut_instance.read_msgpack::>().await?; - - for (sheet_name, version) in local_versions.iter() { - let sheet = vault.sheet(sheet_name).await?; - if let Some(holder) = sheet.holder() - && (holder == &member_id || holder == VAULT_HOST_NAME) - && &sheet.write_count() != version - { - mut_instance.write_msgpack(true).await?; - mut_instance - .write_large_msgpack((sheet_name, sheet.to_data()), 1024u16) - .await?; - } - } - mut_instance.write_msgpack(false).await?; - } - } - - info!("Fetch held status to {}", member_id); - - // Sync Held Info - { - if ctx.is_proc_on_local() { - let workspace = try_get_local_workspace(&ctx)?; - - let Ok(latest_info) = LatestInfo::read_from(LatestInfo::latest_info_path( - workspace.local_path(), - &member_id, - )) - .await - else { - return Err(TcpTargetError::Io("Read latest info failed".to_string())); - }; - - // Collect files that need to know the holder - let mut holder_wants_know = Vec::new(); - for sheet_name in &latest_info.visible_sheets { - if let Ok(sheet_data) = CachedSheet::cached_sheet_data(sheet_name).await { - holder_wants_know - .extend(sheet_data.mapping().values().map(|value| value.id.clone())); - } - } - - // Send request - let mut mut_instance = instance.lock().await; - mut_instance - .write_large_msgpack(&holder_wants_know, 1024u16) - .await?; - - // Receive information and write to local - let result: HashMap< - VirtualFileId, - ( - Option, - VirtualFileVersion, - Vec<(VirtualFileVersion, VirtualFileVersionDescription)>, - ), - > = mut_instance.read_large_msgpack(1024u16).await?; - - // Read configuration file - let path = LatestFileData::data_path(&member_id)?; - let mut latest_file_data: LatestFileData = - LatestFileData::read_from(&path).await.unwrap_or_default(); - - // Write the received information - latest_file_data.update_info(result); - - // Write - LatestFileData::write_to(&latest_file_data, &path).await?; - } - - if ctx.is_proc_on_remote() { - let vault = try_get_vault(&ctx)?; - let mut mut_instance = instance.lock().await; - - // Read the request - let holder_wants_know: Vec = - mut_instance.read_large_msgpack(1024u16).await?; - - // Organize the information - let mut result: HashMap< - VirtualFileId, - ( - Option, - VirtualFileVersion, - Vec<(VirtualFileVersion, VirtualFileVersionDescription)>, - ), - > = HashMap::new(); - for id in holder_wants_know { - let Ok(meta) = vault.virtual_file_meta(&id).await else { - continue; - }; - let holder = if meta.hold_member().is_empty() { - None - } else { - Some(meta.hold_member().clone()) - }; - let latest_version = meta.version_latest(); - - let all_versions = meta.versions(); - let all_descriptions = meta.version_descriptions(); - let histories = all_versions - .iter() - .filter_map(|v| { - let Some(desc) = all_descriptions.get(v) else { - return None; - }; - Some((v.clone(), desc.clone())) - }) - .collect::>(); - - result.insert(id, (holder, latest_version, histories)); - } - - // Send information - mut_instance.write_large_msgpack(&result, 1024u16).await?; - } - } - - // Sync cached sheet to local sheet - if ctx.is_proc_on_local() { - let workspace = try_get_local_workspace(&ctx)?; - let cached_sheet_path = workspace.local_path().join(CLIENT_PATH_CACHED_SHEET); - let local_sheet_path = workspace.local_path().join(CLIENT_PATH_LOCAL_SHEET); - if !local_sheet_path.exists() || !cached_sheet_path.exists() { - // No need to sync - if ctx.is_proc_on_local() { - sign_vault_modified(false).await; - } - return Ok(UpdateToLatestInfoResult::Success); - } - - let cached_sheet_paths = - extract_sheet_names_from_paths(CachedSheet::cached_sheet_paths().await?)?; - - // Match cached sheets and local sheets, and sync content - for (cached_sheet_name, _cached_sheet_path) in cached_sheet_paths { - // Read cached sheet and local sheet - let cached_sheet = CachedSheet::cached_sheet_data(&cached_sheet_name).await?; - let Ok(mut local_sheet) = workspace.local_sheet(&member_id, &cached_sheet_name).await - else { - continue; - }; - - // Read cached id mapping - let Some(cached_sheet_id_mapping) = cached_sheet.id_mapping() else { - continue; - }; - - for (cached_item_id, cached_item_path) in cached_sheet_id_mapping.iter() { - let path_by_id = { local_sheet.path_by_id(cached_item_id).cloned() }; - - // Get local path - let Some(local_path) = path_by_id else { - continue; - }; - - if &local_path == cached_item_path { - continue; - } - - // If path not match, try to move - let move_result = local_sheet.move_mapping(&local_path, cached_item_path); - if let Err(e) = move_result { - match e.kind() { - ErrorKind::AlreadyExists => { - return Ok(UpdateToLatestInfoResult::SyncCachedSheetFail( - SyncCachedSheetFailReason::PathAlreadyExist( - cached_item_path.clone(), - ), - )); - } - _ => return Err(e.into()), - } - } - local_sheet.write().await?; - } - } - } - - if ctx.is_proc_on_local() { - sign_vault_modified(false).await; - } - Ok(UpdateToLatestInfoResult::Success) -} - -/// Extract sheet names from file paths -fn extract_sheet_names_from_paths( - paths: Vec, -) -> Result, std::io::Error> { - let mut result = HashMap::new(); - for p in paths { - let sheet_name = p - .file_stem() - .and_then(|s| s.to_str()) - .map(|s| s.to_string()) - .ok_or_else(|| { - std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid file name") - })?; - result.insert(sheet_name, p); - } - Ok(result) -} diff --git a/data/Cargo.toml b/data/Cargo.toml deleted file mode 100644 index 7506814..0000000 --- a/data/Cargo.toml +++ /dev/null @@ -1,40 +0,0 @@ -[package] -name = "vcs_data" -edition = "2024" -version.workspace = true - -[dependencies] - -# Utils -cfg_file = { path = "../utils/cfg_file", features = ["default"] } -data_struct = { path = "../utils/data_struct" } -sha1_hash = { path = "../utils/sha1_hash" } -tcp_connection = { path = "../utils/tcp_connection" } -string_proc = { path = "../utils/string_proc" } - -# Core -action_system = { path = "../systems/action" } -vcs_docs = { path = "../docs" } - -# Random -rand = "0.9.2" - -# Identity -uuid = { version = "1.18.1", features = ["v4", "serde"] } -whoami = "1.6.1" - -# Serialization -serde = { version = "1.0.228", features = ["derive"] } - -# Async & Networking -tokio = { version = "1.48.0", features = ["full"] } - -# Filesystem -dirs = "6.0.0" -walkdir = "2.5.0" - -# Time -chrono = "0.4.42" - -# Windows API -winapi = { version = "0.3.9", features = ["fileapi", "winbase", "winnt"] } diff --git a/data/src/constants.rs b/data/src/constants.rs deleted file mode 100644 index bc3343f..0000000 --- a/data/src/constants.rs +++ /dev/null @@ -1,140 +0,0 @@ -// ------------------------------------------------------------------------------------- - -// Project -pub const PATH_TEMP: &str = "./.temp/"; - -// Default Port -pub const PORT: u16 = 25331; - -// Vault Host Name -pub const VAULT_HOST_NAME: &str = "host"; - -// Default Virtual File Version and Direction -pub const DEFAULT_VF_VERSION: &str = "0.1.0"; -pub const DEFAULT_VF_DESCRIPTION: &str = "_"; - -// ------------------------------------------------------------------------------------- - -// Prefix -pub const VF_PREFIX: &str = "vf-"; - -// ------------------------------------------------------------------------------------- - -// Suffix -pub const SERVER_SUFFIX_SHEET_FILE: &str = ".st"; -pub const SERVER_SUFFIX_SHEET_FILE_NO_DOT: &str = "st"; - -pub const SERVER_SUFFIX_SHEET_SHARE_FILE: &str = ".sre"; -pub const SERVER_SUFFIX_SHEET_SHARE_FILE_NO_DOT: &str = "sre"; - -pub const SERVER_SUFFIX_MEMBER_INFO: &str = ".json"; -pub const SERVER_SUFFIX_MEMBER_INFO_NO_DOT: &str = "json"; - -pub const SERVER_SUFFIX_VF_META: &str = ".vf"; -pub const SERVER_SUFFIX_VF_META_NO_DOT: &str = "vf"; - -pub const CLIENT_SUFFIX_LATEST_INFO: &str = ".up"; -pub const CLIENT_SUFFIX_LATEST_INFO_NO_DOT: &str = "up"; - -pub const CLIENT_SUFFIX_LATEST_DATA: &str = ".upf"; -pub const CLIENT_SUFFIX_LATEST_DATA_NO_DOT: &str = "upf"; - -pub const CLIENT_SUFFIX_LOCAL_SHEET_FILE: &str = ".lst"; -pub const CLIENT_SUFFIX_LOCAL_SHEET_FILE_NO_DOT: &str = "lst"; - -pub const CLIENT_SUFFIX_CACHED_SHEET_FILE: &str = ".st"; -pub const CLIENT_SUFFIX_CACHED_SHEET_FILE_NO_DOT: &str = "st"; - -// ------------------------------------------------------------------------------------- - -// Keys -pub const KEY_SHEET_NAME: &str = "{sheet_name}"; -pub const KEY_MEMBER_ID: &str = "{member_id}"; -pub const KEY_TEMP_NAME: &str = "{temp_name}"; -pub const KEY_VF_INDEX: &str = "{vf_index}"; -pub const KEY_VF_ID: &str = "{vf_id}"; -pub const KEY_VF_VERSION: &str = "{vf_version}"; -pub const KEY_ACCOUNT: &str = "{account}"; -pub const KEY_SELF_ID: &str = "{self_id}"; -pub const KEY_SHARE_ID: &str = "{share_id}"; - -// ------------------------------------------------------------------------------------- - -// Server -// Server - Vault (Main) -pub const SERVER_FILE_VAULT: &str = "./vault.toml"; - -// Server - Sheets -pub const REF_SHEET_NAME: &str = "ref"; -pub const SERVER_PATH_SHEETS: &str = "./sheets/"; -pub const SERVER_PATH_SHARES: &str = "./sheets/shares/{sheet_name}/"; -pub const SERVER_FILE_SHEET: &str = "./sheets/{sheet_name}.st"; -pub const SERVER_FILE_SHEET_SHARE: &str = "./sheets/shares/{sheet_name}/{share_id}.sre"; - -// Server - Members -pub const SERVER_PATH_MEMBERS: &str = "./members/"; -pub const SERVER_PATH_MEMBER_PUB: &str = "./key/"; -pub const SERVER_FILE_MEMBER_INFO: &str = "./members/{member_id}.json"; -pub const SERVER_FILE_MEMBER_PUB: &str = "./key/{member_id}.pem"; - -// Server - Virtual File Storage -pub const SERVER_PATH_VF_TEMP: &str = "./.temp/{temp_name}"; -pub const SERVER_PATH_VF_ROOT: &str = "./storage/"; -pub const SERVER_PATH_VF_STORAGE: &str = "./storage/{vf_index}/{vf_id}/"; -pub const SERVER_FILE_VF_VERSION_INSTANCE: &str = "./storage/{vf_index}/{vf_id}/{vf_version}.rf"; -pub const SERVER_FILE_VF_META: &str = "./storage/{vf_index}/{vf_id}/meta.vf"; -pub const SERVER_NAME_VF_META: &str = "meta.vf"; - -// Server - Updates -pub const SERVER_FILE_UPDATES: &str = "./.updates.txt"; - -// Server - Service -pub const SERVER_FILE_LOCKFILE: &str = "./.lock"; - -// Server - Documents -pub const SERVER_FILE_README: &str = "./README.md"; - -// ------------------------------------------------------------------------------------- - -// Client -pub const CLIENT_PATH_WORKSPACE_ROOT: &str = "./.jv/"; -pub const CLIENT_FOLDER_WORKSPACE_ROOT_NAME: &str = ".jv"; - -// Client - Workspace (Main) -pub const CLIENT_FILE_WORKSPACE: &str = "./.jv/workspace.toml"; - -// Client - Latest Information -pub const CLIENT_FILE_LATEST_INFO: &str = "./.jv/latest/{account}.up"; -pub const CLIENT_FILE_LATEST_DATA: &str = "./.jv/latest/{account}.upf"; - -// Client - Local -pub const CLIENT_PATH_LOCAL_DRAFT: &str = "./.jv/drafts/{account}/{sheet_name}/"; -pub const CLIENT_PATH_LOCAL_SHEET: &str = "./.jv/sheets/local/"; -pub const CLIENT_FILE_LOCAL_SHEET: &str = "./.jv/sheets/local/{account}/{sheet_name}.lst"; -pub const CLIENT_PATH_CACHED_SHEET: &str = "./.jv/sheets/cached/"; -pub const CLIENT_FILE_CACHED_SHEET: &str = "./.jv/sheets/cached/{sheet_name}.st"; - -pub const CLIENT_FILE_LOCAL_SHEET_NOSET: &str = "./.jv/.temp/wrong.json"; -pub const CLIENT_FILE_MEMBER_HELD_NOSET: &str = "./.jv/.temp/wrong.json"; -pub const CLIENT_FILE_LATEST_INFO_NOSET: &str = "./.jv/.temp/wrong.json"; - -// Client - Other -pub const CLIENT_FILE_IGNOREFILES: &str = "IGNORE_RULES.toml"; -pub const CLIENT_FILE_TODOLIST: &str = "./SETUP.md"; -pub const CLIENT_FILE_GITIGNORE: &str = "./.jv/.gitignore"; -pub const CLIENT_CONTENT_GITIGNORE: &str = "# Git support for JVCS Workspace - -# Ignore cached datas -/sheets/cached/ -/latest/ - -.vault_modified"; -pub const CLIENT_FILE_VAULT_MODIFIED: &str = "./.jv/.vault_modified"; -pub const CLIENT_FILE_TEMP_FILE: &str = "./.jv/.temp/download/{temp_name}"; - -// ------------------------------------------------------------------------------------- - -// User - Verify (Documents path) -pub const USER_FILE_ACCOUNTS: &str = "./accounts/"; -pub const USER_FILE_KEY: &str = "./accounts/{self_id}_private.pem"; -pub const USER_FILE_MEMBER: &str = "./accounts/{self_id}.toml"; diff --git a/data/src/data.rs b/data/src/data.rs deleted file mode 100644 index ed9383a..0000000 --- a/data/src/data.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub mod local; -pub mod member; -pub mod sheet; -pub mod user; -pub mod vault; diff --git a/data/src/data/local.rs b/data/src/data/local.rs deleted file mode 100644 index d4115c6..0000000 --- a/data/src/data/local.rs +++ /dev/null @@ -1,266 +0,0 @@ -use std::{ - collections::HashMap, - env::current_dir, - path::{Path, PathBuf}, - sync::Arc, -}; - -use cfg_file::config::ConfigFile; -use string_proc::format_path::format_path; -use tokio::{fs, sync::Mutex}; -use vcs_docs::docs::READMES_LOCAL_WORKSPACE_TODOLIST; - -use crate::{ - constants::{ - CLIENT_CONTENT_GITIGNORE, CLIENT_FILE_GITIGNORE, CLIENT_FILE_LOCAL_SHEET, - CLIENT_FILE_TODOLIST, CLIENT_FILE_WORKSPACE, CLIENT_FOLDER_WORKSPACE_ROOT_NAME, - CLIENT_PATH_LOCAL_SHEET, CLIENT_SUFFIX_LOCAL_SHEET_FILE, KEY_ACCOUNT, KEY_SHEET_NAME, - }, - data::{ - local::{ - local_sheet::{LocalSheet, LocalSheetData, LocalSheetPathBuf}, - workspace_config::LocalConfig, - }, - member::MemberId, - sheet::SheetName, - }, - env::{current_local_path, find_local_path}, -}; - -pub mod align_tasks; -pub mod cached_sheet; -pub mod latest_file_data; -pub mod latest_info; -pub mod local_files; -pub mod local_sheet; -pub mod modified_status; -pub mod workspace_analyzer; -pub mod workspace_config; - -pub struct LocalWorkspace { - config: Arc>, - local_path: PathBuf, -} - -impl LocalWorkspace { - /// Get the path of the local workspace. - pub fn local_path(&self) -> &PathBuf { - &self.local_path - } - - /// Initialize local workspace. - pub fn init(config: LocalConfig, local_path: impl Into) -> Option { - let local_path = find_local_path(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: Arc::new(Mutex::new(config)), - local_path, - }) - } - - /// Setup local workspace - pub async fn setup_local_workspace( - local_path: impl Into, - ) -> Result<(), std::io::Error> { - let local_path: PathBuf = local_path.into(); - - // Ensure directory is empty - if local_path.exists() && local_path.read_dir()?.next().is_some() { - return Err(std::io::Error::new( - std::io::ErrorKind::DirectoryNotEmpty, - "DirectoryNotEmpty", - )); - } - - // 1. Setup config - let config = LocalConfig::default(); - LocalConfig::write_to(&config, local_path.join(CLIENT_FILE_WORKSPACE)).await?; - - // 2. Setup SETUP.md - let readme_content = READMES_LOCAL_WORKSPACE_TODOLIST.trim().to_string(); - fs::write(local_path.join(CLIENT_FILE_TODOLIST), readme_content).await?; - - // 3. Setup .gitignore - fs::write( - local_path.join(CLIENT_FILE_GITIGNORE), - CLIENT_CONTENT_GITIGNORE, - ) - .await?; - - // On Windows, set the .jv directory as hidden - let jv_dir = local_path.join(CLIENT_FOLDER_WORKSPACE_ROOT_NAME); - let _ = hide_folder::hide_folder(&jv_dir); - - 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?; - Ok(()) - } - - /// Get the path to a local sheet. - pub fn local_sheet_path(&self, member: &MemberId, sheet: &SheetName) -> PathBuf { - self.local_path.join( - CLIENT_FILE_LOCAL_SHEET - .replace(KEY_ACCOUNT, member) - .replace(KEY_SHEET_NAME, sheet), - ) - } - - /// Read or initialize a local sheet. - pub async fn local_sheet( - &self, - member: &MemberId, - sheet: &SheetName, - ) -> Result, std::io::Error> { - let local_sheet_path = self.local_sheet_path(member, sheet); - - if !local_sheet_path.exists() { - let sheet_data = LocalSheetData { - mapping: HashMap::new(), - vfs: HashMap::new(), - }; - LocalSheetData::write_to(&sheet_data, local_sheet_path).await?; - return Ok(LocalSheet { - local_workspace: self, - member: member.clone(), - sheet_name: sheet.clone(), - data: sheet_data, - }); - } - - let data = LocalSheetData::read_from(&local_sheet_path).await?; - let local_sheet = LocalSheet { - local_workspace: self, - member: member.clone(), - sheet_name: sheet.clone(), - data, - }; - - Ok(local_sheet) - } - - /// Collect all theet names - pub async fn local_sheet_paths(&self) -> Result, std::io::Error> { - let local_sheet_path = self.local_path.join(CLIENT_PATH_LOCAL_SHEET); - let mut sheet_paths = Vec::new(); - - async fn collect_sheet_paths( - dir: &Path, - suffix: &str, - paths: &mut Vec, - ) -> Result<(), std::io::Error> { - if dir.is_dir() { - let mut entries = fs::read_dir(dir).await?; - while let Some(entry) = entries.next_entry().await? { - let path = entry.path(); - - if path.is_dir() { - Box::pin(collect_sheet_paths(&path, suffix, paths)).await?; - } else if path.is_file() - && let Some(extension) = path.extension() - && extension == suffix.trim_start_matches('.') - { - let formatted_path = format_path(path)?; - paths.push(formatted_path); - } - } - } - Ok(()) - } - - collect_sheet_paths( - &local_sheet_path, - CLIENT_SUFFIX_LOCAL_SHEET_FILE, - &mut sheet_paths, - ) - .await?; - Ok(sheet_paths) - } -} - -mod hide_folder { - use std::io; - use std::path::Path; - - #[cfg(windows)] - use std::os::windows::ffi::OsStrExt; - #[cfg(windows)] - use winapi::um::fileapi::{GetFileAttributesW, INVALID_FILE_ATTRIBUTES, SetFileAttributesW}; - - pub fn hide_folder(path: &Path) -> io::Result<()> { - if !path.is_dir() { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Path must be a directory", - )); - } - - if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) { - if !file_name.starts_with('.') { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Directory name must start with '.'", - )); - } - } else { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Invalid directory name", - )); - } - - hide_folder_impl(path) - } - - #[cfg(windows)] - fn hide_folder_impl(path: &Path) -> io::Result<()> { - // Convert to Windows wide string format - let path_str: Vec = path.as_os_str().encode_wide().chain(Some(0)).collect(); - - // Get current attributes - let attrs = unsafe { GetFileAttributesW(path_str.as_ptr()) }; - if attrs == INVALID_FILE_ATTRIBUTES { - return Err(io::Error::last_os_error()); - } - - // Add hidden attribute flag - let new_attrs = attrs | winapi::um::winnt::FILE_ATTRIBUTE_HIDDEN; - - // Set new attributes - let success = unsafe { SetFileAttributesW(path_str.as_ptr(), new_attrs) }; - if success == 0 { - return Err(io::Error::last_os_error()); - } - - Ok(()) - } - - #[cfg(unix)] - fn hide_folder_impl(_path: &Path) -> io::Result<()> { - Ok(()) - } - - #[cfg(not(any(windows, unix)))] - fn hide_folder_impl(_path: &Path) -> io::Result<()> { - Err(io::Error::new( - io::ErrorKind::Unsupported, - "Unsupported operating system", - )) - } -} diff --git a/data/src/data/local/align_tasks.rs b/data/src/data/local/align_tasks.rs deleted file mode 100644 index b72804c..0000000 --- a/data/src/data/local/align_tasks.rs +++ /dev/null @@ -1,110 +0,0 @@ -use std::{ - collections::{HashMap, HashSet}, - path::PathBuf, -}; - -use data_struct::data_sort::quick_sort_with_cmp; - -use crate::data::local::workspace_analyzer::AnalyzeResult; - -pub type AlignTaskName = String; -pub type AlignPathBuf = PathBuf; -pub type AlignLostPathBuf = PathBuf; -pub type AlignCreatedPathBuf = PathBuf; - -pub struct AlignTasks { - pub created: Vec<(AlignTaskName, AlignPathBuf)>, - pub lost: Vec<(AlignTaskName, AlignPathBuf)>, - pub moved: Vec<(AlignTaskName, (AlignLostPathBuf, AlignCreatedPathBuf))>, - pub erased: Vec<(AlignTaskName, AlignPathBuf)>, -} - -impl AlignTasks { - pub fn clone_from_analyze_result(result: &AnalyzeResult) -> Self { - AlignTasks { - created: path_hash_set_sort_helper(result.created.clone(), "created"), - lost: path_hash_set_sort_helper(result.lost.clone(), "lost"), - moved: path_hash_map_sort_helper(result.moved.clone(), "moved"), - erased: path_hash_set_sort_helper(result.erased.clone(), "erased"), - } - } - - pub fn from_analyze_result(result: AnalyzeResult) -> Self { - AlignTasks { - created: path_hash_set_sort_helper(result.created, "created"), - lost: path_hash_set_sort_helper(result.lost, "lost"), - moved: path_hash_map_sort_helper(result.moved, "moved"), - erased: path_hash_set_sort_helper(result.erased, "erased"), - } - } -} - -fn path_hash_set_sort_helper( - hash_set: HashSet, - prefix: impl Into, -) -> Vec<(String, PathBuf)> { - let prefix_str = prefix.into(); - let mut vec: Vec<(String, PathBuf)> = hash_set - .into_iter() - .map(|path| { - let hash = sha1_hash::calc_sha1_string(path.to_string_lossy()); - let hash_prefix: String = hash.chars().take(8).collect(); - let name = format!("{}:{}", prefix_str, hash_prefix); - (name, path) - }) - .collect(); - - quick_sort_with_cmp(&mut vec, false, |a, b| { - // Compare by path depth first - let a_depth = a.1.components().count(); - let b_depth = b.1.components().count(); - - if a_depth != b_depth { - return if a_depth < b_depth { -1 } else { 1 }; - } - - // If same depth, compare lexicographically - match a.1.cmp(&b.1) { - std::cmp::Ordering::Less => -1, - std::cmp::Ordering::Equal => 0, - std::cmp::Ordering::Greater => 1, - } - }); - - vec -} - -fn path_hash_map_sort_helper( - hash_map: HashMap, - prefix: impl Into, -) -> Vec<(String, (PathBuf, PathBuf))> { - let prefix_str = prefix.into(); - let mut vec: Vec<(String, (PathBuf, PathBuf))> = hash_map - .into_values() - .map(|(path1, path2)| { - let hash = sha1_hash::calc_sha1_string(path1.to_string_lossy()); - let hash_prefix: String = hash.chars().take(8).collect(); - let name = format!("{}:{}", prefix_str, hash_prefix); - (name, (path1, path2)) - }) - .collect(); - - quick_sort_with_cmp(&mut vec, false, |a, b| { - // Compare by first PathBuf's path depth first - let a_depth = a.1.0.components().count(); - let b_depth = b.1.0.components().count(); - - if a_depth != b_depth { - return if a_depth < b_depth { -1 } else { 1 }; - } - - // If same depth, compare lexicographically by first PathBuf - match a.1.0.cmp(&b.1.0) { - std::cmp::Ordering::Less => -1, - std::cmp::Ordering::Equal => 0, - std::cmp::Ordering::Greater => 1, - } - }); - - vec -} diff --git a/data/src/data/local/cached_sheet.rs b/data/src/data/local/cached_sheet.rs deleted file mode 100644 index 46b390f..0000000 --- a/data/src/data/local/cached_sheet.rs +++ /dev/null @@ -1,94 +0,0 @@ -use std::{io::Error, path::PathBuf}; - -use cfg_file::config::ConfigFile; -use string_proc::{format_path::format_path, snake_case}; -use tokio::fs; - -use crate::{ - constants::{ - CLIENT_FILE_CACHED_SHEET, CLIENT_PATH_CACHED_SHEET, CLIENT_SUFFIX_CACHED_SHEET_FILE, - KEY_SHEET_NAME, - }, - data::sheet::{SheetData, SheetName}, - env::current_local_path, -}; - -pub type CachedSheetPathBuf = PathBuf; - -/// # Cached Sheet -/// The cached sheet is a read-only version cloned from the upstream repository to the local environment, -/// automatically generated during update operations, -/// which records the latest Sheet information stored locally to accelerate data access and reduce network requests. -pub struct CachedSheet; - -impl CachedSheet { - /// Read the cached sheet data. - pub async fn cached_sheet_data(sheet_name: &SheetName) -> Result { - let sheet_name = snake_case!(sheet_name.clone()); - - let Some(path) = Self::cached_sheet_path(sheet_name) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "Local workspace not found!", - )); - }; - let data = SheetData::read_from(path).await?; - Ok(data) - } - - /// Get the path to the cached sheet file. - pub fn cached_sheet_path(sheet_name: SheetName) -> Option { - let current_workspace = current_local_path()?; - Some( - current_workspace - .join(CLIENT_FILE_CACHED_SHEET.replace(KEY_SHEET_NAME, &sheet_name.to_string())), - ) - } - - /// Get all cached sheet names - pub async fn cached_sheet_names() -> Result, std::io::Error> { - let mut dir = fs::read_dir(CLIENT_PATH_CACHED_SHEET).await?; - let mut sheet_names = Vec::new(); - - while let Some(entry) = dir.next_entry().await? { - let path = entry.path(); - - if path.is_file() - && let Some(file_name) = path.file_name().and_then(|n| n.to_str()) - && file_name.ends_with(CLIENT_SUFFIX_CACHED_SHEET_FILE) - { - let name_without_ext = file_name - .trim_end_matches(CLIENT_SUFFIX_CACHED_SHEET_FILE) - .to_string(); - sheet_names.push(name_without_ext); - } - } - - Ok(sheet_names) - } - - /// Get all cached sheet paths - pub async fn cached_sheet_paths() -> Result, std::io::Error> { - let mut dir = fs::read_dir(CLIENT_PATH_CACHED_SHEET).await?; - let mut sheet_paths = Vec::new(); - let Some(workspace_path) = current_local_path() else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "Local workspace not found!", - )); - }; - - while let Some(entry) = dir.next_entry().await? { - let path = entry.path(); - - if path.is_file() - && let Some(file_name) = path.file_name().and_then(|n| n.to_str()) - && file_name.ends_with(CLIENT_SUFFIX_CACHED_SHEET_FILE) - { - sheet_paths.push(format_path(workspace_path.join(path))?); - } - } - - Ok(sheet_paths) - } -} diff --git a/data/src/data/local/latest_file_data.rs b/data/src/data/local/latest_file_data.rs deleted file mode 100644 index f9b3aeb..0000000 --- a/data/src/data/local/latest_file_data.rs +++ /dev/null @@ -1,103 +0,0 @@ -use std::{collections::HashMap, io::Error, path::PathBuf}; - -use cfg_file::ConfigFile; -use serde::{Deserialize, Serialize}; - -use crate::{ - constants::{CLIENT_FILE_LATEST_DATA, CLIENT_FILE_MEMBER_HELD_NOSET, KEY_ACCOUNT}, - data::{ - member::MemberId, - vault::virtual_file::{VirtualFileId, VirtualFileVersion, VirtualFileVersionDescription}, - }, - env::current_local_path, -}; - -/// # Latest file data -/// Records the file holder and the latest version for permission and update checks -#[derive(Debug, Default, Clone, Serialize, Deserialize, ConfigFile)] -#[cfg_file(path = CLIENT_FILE_MEMBER_HELD_NOSET)] -pub struct LatestFileData { - /// File holding status - #[serde(rename = "held")] - held_status: HashMap, - - /// File version - #[serde(rename = "ver")] - versions: HashMap, - - /// File histories and descriptions - #[serde(rename = "his")] - histories: HashMap>, -} - -#[derive(Debug, Default, Clone, Serialize, Deserialize)] -pub enum HeldStatus { - #[serde(rename = "Hold")] - HeldWith(MemberId), // Held, status changes are sync to the client - - #[serde(rename = "None")] - NotHeld, // Not held, status changes are sync to the client - - #[default] - #[serde(rename = "Unknown")] - WantedToKnow, // Holding status is unknown, notify server must inform client -} - -impl LatestFileData { - /// Get the path to the file holding the held status information for the given member. - pub fn data_path(account: &MemberId) -> Result { - let Some(local_path) = current_local_path() else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "Workspace not found.", - )); - }; - Ok(local_path.join(CLIENT_FILE_LATEST_DATA.replace(KEY_ACCOUNT, account))) - } - - /// Get the member who holds the file with the given ID. - pub fn file_holder(&self, vfid: &VirtualFileId) -> Option<&MemberId> { - self.held_status.get(vfid).and_then(|status| match status { - HeldStatus::HeldWith(id) => Some(id), - _ => None, - }) - } - - /// Get the version of the file with the given ID. - pub fn file_version(&self, vfid: &VirtualFileId) -> Option<&VirtualFileVersion> { - self.versions.get(vfid) - } - - /// Get the version of the file with the given ID. - pub fn file_histories( - &self, - vfid: &VirtualFileId, - ) -> Option<&Vec<(VirtualFileVersion, VirtualFileVersionDescription)>> { - self.histories.get(vfid) - } - - /// Update the held status of the files. - pub fn update_info( - &mut self, - map: HashMap< - VirtualFileId, - ( - Option, - VirtualFileVersion, - Vec<(VirtualFileVersion, VirtualFileVersionDescription)>, - ), - >, - ) { - for (vfid, (member_id, version, desc)) in map { - self.held_status.insert( - vfid.clone(), - match member_id { - Some(member_id) => HeldStatus::HeldWith(member_id), - None => HeldStatus::NotHeld, - }, - ); - self.versions.insert(vfid.clone(), version); - self.histories.insert(vfid, desc); - } - } -} diff --git a/data/src/data/local/latest_info.rs b/data/src/data/local/latest_info.rs deleted file mode 100644 index 5748793..0000000 --- a/data/src/data/local/latest_info.rs +++ /dev/null @@ -1,81 +0,0 @@ -use std::{ - collections::{HashMap, HashSet}, - path::{Path, PathBuf}, - time::SystemTime, -}; - -use cfg_file::ConfigFile; -use serde::{Deserialize, Serialize}; - -use crate::{ - constants::{CLIENT_FILE_LATEST_INFO, CLIENT_FILE_LATEST_INFO_NOSET, KEY_ACCOUNT}, - data::{ - member::{Member, MemberId}, - sheet::{SheetData, SheetName, SheetPathBuf}, - vault::{ - mapping_share::{Share, SheetShareId}, - virtual_file::VirtualFileId, - }, - }, -}; - -/// # Latest Info -/// Locally cached latest information, -/// used to cache personal information from upstream for querying and quickly retrieving member information. -#[derive(Default, Serialize, Deserialize, ConfigFile)] -#[cfg_file(path = CLIENT_FILE_LATEST_INFO_NOSET)] -pub struct LatestInfo { - // Sheets - /// Visible sheets, - /// indicating which sheets I can edit - #[serde(rename = "my")] - pub visible_sheets: Vec, - - /// Invisible sheets, - /// indicating which sheets I can export files to (these sheets are not readable to me) - #[serde(rename = "others")] - pub invisible_sheets: Vec, - - /// Reference sheets, - /// indicating sheets owned by the host, visible to everyone, - /// but only the host can modify or add mappings within them - #[serde(rename = "refsheets")] - pub reference_sheets: HashSet, - - /// Reference sheet data, indicating what files I can get from the reference sheet - #[serde(rename = "ref")] - pub ref_sheet_content: SheetData, - - /// Reverse mapping from virtual file IDs to actual paths in reference sheets - #[serde(rename = "ref_vfs")] - pub ref_sheet_vfs_mapping: HashMap, - - /// Shares in my sheets, indicating which external merge requests have entries that I can view - #[serde(rename = "shares")] - pub shares_in_my_sheets: HashMap>, - - /// Update instant - #[serde(rename = "update")] - pub update_instant: Option, - - // Members - /// All member information of the vault, allowing me to contact them more conveniently - #[serde(rename = "members")] - pub vault_members: Vec, -} - -impl LatestInfo { - /// Get the path to the latest info file for a given workspace and member ID - pub fn latest_info_path(local_workspace_path: &Path, member_id: &MemberId) -> PathBuf { - local_workspace_path.join(CLIENT_FILE_LATEST_INFO.replace(KEY_ACCOUNT, member_id)) - } -} - -#[derive(Default, Serialize, Deserialize)] -pub struct SheetInfo { - #[serde(rename = "name")] - pub sheet_name: SheetName, - - #[serde(rename = "holder")] - pub holder_name: Option, -} diff --git a/data/src/data/local/local_files.rs b/data/src/data/local/local_files.rs deleted file mode 100644 index 9cc244f..0000000 --- a/data/src/data/local/local_files.rs +++ /dev/null @@ -1,148 +0,0 @@ -use std::path::{Path, PathBuf}; - -use string_proc::format_path::format_path; -use tokio::fs; - -use crate::constants::CLIENT_FOLDER_WORKSPACE_ROOT_NAME; - -pub struct RelativeFiles { - pub(crate) files: Vec, -} - -impl IntoIterator for RelativeFiles { - type Item = PathBuf; - type IntoIter = std::vec::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.files.into_iter() - } -} - -impl RelativeFiles { - pub fn iter(&self) -> std::slice::Iter<'_, PathBuf> { - self.files.iter() - } -} - -/// Read the relative paths within the project from the input file list -pub async fn get_relative_paths(local_path: &PathBuf, paths: &[PathBuf]) -> Option { - // Get Relative Paths - let Ok(paths) = format_input_paths_and_ignore_outside_paths(local_path, paths).await else { - return None; - }; - let files: Vec = abs_paths_to_abs_files(paths).await; - let Ok(files) = parse_to_relative(local_path, files) else { - return None; - }; - Some(RelativeFiles { files }) -} - -/// Normalize the input paths -async fn format_input_paths( - local_path: &Path, - track_files: &[PathBuf], -) -> Result, std::io::Error> { - let current_dir = local_path; - - let mut real_paths = Vec::new(); - for file in track_files { - let path = current_dir.join(file); - - // Skip paths that contain .jv directories - if path.components().any(|component| { - if let std::path::Component::Normal(name) = component { - name.to_str() == Some(CLIENT_FOLDER_WORKSPACE_ROOT_NAME) - } else { - false - } - }) { - continue; - } - - match format_path(path) { - Ok(path) => real_paths.push(path), - Err(e) => { - return Err(std::io::Error::new( - std::io::ErrorKind::InvalidData, - format!("Failed to format path: {}", e), - )); - } - } - } - - Ok(real_paths) -} - -/// Ignore files outside the workspace -async fn format_input_paths_and_ignore_outside_paths( - local_path: &PathBuf, - files: &[PathBuf], -) -> Result, std::io::Error> { - let result = format_input_paths(local_path, files).await?; - let result: Vec = result - .into_iter() - .filter(|path| path.starts_with(local_path)) - .collect(); - Ok(result) -} - -/// Normalize the input paths to relative paths -fn parse_to_relative( - local_dir: &PathBuf, - files: Vec, -) -> Result, std::io::Error> { - let result: Result, _> = files - .iter() - .map(|p| { - p.strip_prefix(local_dir) - .map(|relative| relative.to_path_buf()) - .map_err(|_| { - std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "Path prefix stripping failed", - ) - }) - }) - .collect(); - - result -} - -/// Convert absolute paths to absolute file paths, expanding directories to their contained files -async fn abs_paths_to_abs_files(paths: Vec) -> Vec { - let mut files = Vec::new(); - - for path in paths { - if !path.exists() { - continue; - } - - let metadata = match fs::metadata(&path).await { - Ok(meta) => meta, - Err(_) => continue, - }; - - if metadata.is_file() { - files.push(path); - } else if metadata.is_dir() { - let walker = walkdir::WalkDir::new(&path); - for entry in walker.into_iter().filter_map(|e| e.ok()) { - if entry.path().components().any(|component| { - if let std::path::Component::Normal(name) = component { - name == CLIENT_FOLDER_WORKSPACE_ROOT_NAME - } else { - false - } - }) { - continue; - } - - if entry.file_type().is_file() { - files.push(entry.path().to_path_buf()); - } - } - } - } - - files -} diff --git a/data/src/data/local/local_sheet.rs b/data/src/data/local/local_sheet.rs deleted file mode 100644 index b9c29f5..0000000 --- a/data/src/data/local/local_sheet.rs +++ /dev/null @@ -1,439 +0,0 @@ -use std::{collections::HashMap, io::Error, path::PathBuf, time::SystemTime}; - -use ::serde::{Deserialize, Serialize}; -use cfg_file::{ConfigFile, config::ConfigFile}; -use string_proc::format_path::format_path; - -use crate::{ - constants::CLIENT_FILE_LOCAL_SHEET_NOSET, - data::{ - local::LocalWorkspace, - member::MemberId, - sheet::SheetName, - vault::virtual_file::{VirtualFileId, VirtualFileVersion, VirtualFileVersionDescription}, - }, -}; - -pub type LocalFilePathBuf = PathBuf; -pub type LocalSheetPathBuf = PathBuf; - -/// # Local Sheet -/// Local sheet information, used to record metadata of actual local files, -/// to compare with upstream information for more optimized file submission, -/// and to determine whether files need to be updated or submitted. -pub struct LocalSheet<'a> { - pub(crate) local_workspace: &'a LocalWorkspace, - pub(crate) member: MemberId, - pub(crate) sheet_name: String, - pub(crate) data: LocalSheetData, -} - -impl<'a> LocalSheet<'a> { - /// Create a new LocalSheet instance - pub fn new( - local_workspace: &'a LocalWorkspace, - member: MemberId, - sheet_name: String, - data: LocalSheetData, - ) -> Self { - Self { - local_workspace, - member, - sheet_name, - data, - } - } -} - -#[derive(Debug, Default, Serialize, Deserialize, ConfigFile, Clone)] -#[cfg_file(path = CLIENT_FILE_LOCAL_SHEET_NOSET)] // Do not use LocalSheet::write or LocalSheet::read -pub struct LocalSheetData { - /// Local file path to metadata mapping. - #[serde(rename = "map")] - pub(crate) mapping: HashMap, - - #[serde(rename = "vfs")] - pub(crate) vfs: HashMap, -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct LocalMappingMetadata { - /// Hash value generated immediately after the file is downloaded to the local workspace - #[serde(rename = "base_hash")] - pub(crate) hash_when_updated: String, - - /// Time when the file was downloaded to the local workspace - #[serde(rename = "time")] - pub(crate) time_when_updated: SystemTime, - - /// Size of the file when downloaded to the local workspace - #[serde(rename = "size")] - pub(crate) size_when_updated: u64, - - /// Version description when the file was downloaded to the local workspace - #[serde(rename = "desc")] - pub(crate) version_desc_when_updated: VirtualFileVersionDescription, - - /// Version when the file was downloaded to the local workspace - #[serde(rename = "ver")] - pub(crate) version_when_updated: VirtualFileVersion, - - /// Virtual file ID corresponding to the local path - #[serde(rename = "id")] - pub(crate) mapping_vfid: VirtualFileId, - - /// Latest modifiy check time - #[serde(rename = "check_time")] - pub(crate) last_modify_check_time: SystemTime, - - /// Latest modifiy check result - #[serde(rename = "modified")] - pub(crate) last_modify_check_result: bool, - - /// Latest modifiy check hash result - #[serde(rename = "current_hash")] - pub(crate) last_modify_check_hash: Option, -} - -impl LocalSheetData { - /// Wrap LocalSheetData into LocalSheet with workspace, member, and sheet name - pub fn wrap_to_local_sheet<'a>( - self, - workspace: &'a LocalWorkspace, - member: MemberId, - sheet_name: SheetName, - ) -> LocalSheet<'a> { - LocalSheet { - local_workspace: workspace, - member, - sheet_name, - data: self, - } - } -} - -impl LocalMappingMetadata { - /// Create a new MappingMetaData instance - #[allow(clippy::too_many_arguments)] - pub fn new( - hash_when_updated: String, - time_when_updated: SystemTime, - size_when_updated: u64, - version_desc_when_updated: VirtualFileVersionDescription, - version_when_updated: VirtualFileVersion, - mapping_vfid: VirtualFileId, - last_modifiy_check_time: SystemTime, - last_modifiy_check_result: bool, - ) -> Self { - Self { - hash_when_updated, - time_when_updated, - size_when_updated, - version_desc_when_updated, - version_when_updated, - mapping_vfid, - last_modify_check_time: last_modifiy_check_time, - last_modify_check_result: last_modifiy_check_result, - last_modify_check_hash: None, - } - } - - /// Getter for hash_when_updated - pub fn hash_when_updated(&self) -> &String { - &self.hash_when_updated - } - - /// Setter for hash_when_updated - pub fn set_hash_when_updated(&mut self, hash: String) { - self.hash_when_updated = hash; - } - - /// Getter for date_when_updated - pub fn time_when_updated(&self) -> &SystemTime { - &self.time_when_updated - } - - /// Setter for time_when_updated - pub fn set_time_when_updated(&mut self, time: SystemTime) { - self.time_when_updated = time; - } - - /// Getter for size_when_updated - pub fn size_when_updated(&self) -> u64 { - self.size_when_updated - } - - /// Setter for size_when_updated - pub fn set_size_when_updated(&mut self, size: u64) { - self.size_when_updated = size; - } - - /// Getter for version_desc_when_updated - pub fn version_desc_when_updated(&self) -> &VirtualFileVersionDescription { - &self.version_desc_when_updated - } - - /// Setter for version_desc_when_updated - pub fn set_version_desc_when_updated(&mut self, version_desc: VirtualFileVersionDescription) { - self.version_desc_when_updated = version_desc; - } - - /// Getter for version_when_updated - pub fn version_when_updated(&self) -> &VirtualFileVersion { - &self.version_when_updated - } - - /// Setter for version_when_updated - pub fn set_version_when_updated(&mut self, version: VirtualFileVersion) { - self.version_when_updated = version; - } - - /// Getter for mapping_vfid - pub fn mapping_vfid(&self) -> &VirtualFileId { - &self.mapping_vfid - } - - /// Setter for mapping_vfid - pub fn set_mapping_vfid(&mut self, vfid: VirtualFileId) { - self.mapping_vfid = vfid; - } - - /// Getter for last_modifiy_check_time - pub fn last_modifiy_check_time(&self) -> &SystemTime { - &self.last_modify_check_time - } - - /// Setter for last_modifiy_check_time - pub fn set_last_modifiy_check_time(&mut self, time: SystemTime) { - self.last_modify_check_time = time; - } - - /// Getter for last_modifiy_check_result - pub fn last_modifiy_check_result(&self) -> bool { - self.last_modify_check_result - } - - /// Setter for last_modifiy_check_result - pub fn set_last_modifiy_check_result(&mut self, result: bool) { - self.last_modify_check_result = result; - } - - /// Getter for last_modifiy_check_hash - pub fn last_modifiy_check_hash(&self) -> &Option { - &self.last_modify_check_hash - } - - /// Setter for last_modifiy_check_hash - pub fn set_last_modifiy_check_hash(&mut self, hash: Option) { - self.last_modify_check_hash = hash; - } -} - -impl Default for LocalMappingMetadata { - fn default() -> Self { - Self { - hash_when_updated: Default::default(), - time_when_updated: SystemTime::now(), - size_when_updated: Default::default(), - version_desc_when_updated: Default::default(), - version_when_updated: Default::default(), - mapping_vfid: Default::default(), - last_modify_check_time: SystemTime::now(), - last_modify_check_result: false, - last_modify_check_hash: None, - } - } -} - -mod instant_serde { - use serde::{self, Deserialize, Deserializer, Serializer}; - use tokio::time::Instant; - - pub fn serialize(instant: &Instant, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u64(instant.elapsed().as_secs()) - } - - pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let secs = u64::deserialize(deserializer)?; - Ok(Instant::now() - std::time::Duration::from_secs(secs)) - } -} - -impl<'a> From<&'a LocalSheet<'a>> for &'a LocalSheetData { - fn from(sheet: &'a LocalSheet<'a>) -> Self { - &sheet.data - } -} - -impl LocalSheetData { - /// Add mapping to local sheet data - pub fn add_mapping( - &mut self, - path: &LocalFilePathBuf, - mapping: LocalMappingMetadata, - ) -> Result<(), std::io::Error> { - let path = format_path(path)?; - if self.mapping.contains_key(&path) || self.vfs.contains_key(&mapping.mapping_vfid) { - return Err(Error::new( - std::io::ErrorKind::AlreadyExists, - "Mapping already exists", - )); - } - - self.mapping.insert(path.clone(), mapping.clone()); - self.vfs.insert(mapping.mapping_vfid.clone(), path); - Ok(()) - } - - /// Move mapping to other path - pub fn move_mapping( - &mut self, - from: &LocalFilePathBuf, - to: &LocalFilePathBuf, - ) -> Result<(), std::io::Error> { - let from = format_path(from)?; - let to = format_path(to)?; - if self.mapping.contains_key(&to) { - return Err(Error::new( - std::io::ErrorKind::AlreadyExists, - "To path already exists.", - )); - } - - let Some(old_value) = self.mapping.remove(&from) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "From path is not found.", - )); - }; - - // Update vfs mapping - self.vfs.insert(old_value.mapping_vfid.clone(), to.clone()); - self.mapping.insert(to, old_value); - - Ok(()) - } - - /// Remove mapping from local sheet - pub fn remove_mapping( - &mut self, - path: &LocalFilePathBuf, - ) -> Result { - let path = format_path(path)?; - match self.mapping.remove(&path) { - Some(mapping) => { - self.vfs.remove(&mapping.mapping_vfid); - Ok(mapping) - } - None => Err(Error::new( - std::io::ErrorKind::NotFound, - "Path is not found.", - )), - } - } - - /// Get immutable mapping data - pub fn mapping_data( - &self, - path: &LocalFilePathBuf, - ) -> Result<&LocalMappingMetadata, std::io::Error> { - let path = format_path(path)?; - let Some(data) = self.mapping.get(&path) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "Path is not found.", - )); - }; - Ok(data) - } - - /// Get mutable mapping data - pub fn mapping_data_mut( - &mut self, - path: &LocalFilePathBuf, - ) -> Result<&mut LocalMappingMetadata, std::io::Error> { - let path = format_path(path)?; - let Some(data) = self.mapping.get_mut(&path) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "Path is not found.", - )); - }; - Ok(data) - } - - /// Get path by VirtualFileId - pub fn path_by_id(&self, vfid: &VirtualFileId) -> Option<&PathBuf> { - self.vfs.get(vfid) - } -} - -impl<'a> LocalSheet<'a> { - /// Add mapping to local sheet data - pub fn add_mapping( - &mut self, - path: &LocalFilePathBuf, - mapping: LocalMappingMetadata, - ) -> Result<(), std::io::Error> { - self.data.add_mapping(path, mapping) - } - - /// Move mapping to other path - pub fn move_mapping( - &mut self, - from: &LocalFilePathBuf, - to: &LocalFilePathBuf, - ) -> Result<(), std::io::Error> { - self.data.move_mapping(from, to) - } - - /// Remove mapping from local sheet - pub fn remove_mapping( - &mut self, - path: &LocalFilePathBuf, - ) -> Result { - self.data.remove_mapping(path) - } - - /// Get immutable mapping data - pub fn mapping_data( - &self, - path: &LocalFilePathBuf, - ) -> Result<&LocalMappingMetadata, std::io::Error> { - self.data.mapping_data(path) - } - - /// Get mutable mapping data - pub fn mapping_data_mut( - &mut self, - path: &LocalFilePathBuf, - ) -> Result<&mut LocalMappingMetadata, std::io::Error> { - self.data.mapping_data_mut(path) - } - - /// Write the sheet to disk - pub async fn write(&mut self) -> Result<(), std::io::Error> { - let path = self - .local_workspace - .local_sheet_path(&self.member, &self.sheet_name); - self.write_to_path(path).await - } - - /// Write the sheet to custom path - pub async fn write_to_path(&mut self, path: impl Into) -> Result<(), std::io::Error> { - let path = path.into(); - LocalSheetData::write_to(&self.data, path).await?; - Ok(()) - } - - /// Get path by VirtualFileId - pub fn path_by_id(&self, vfid: &VirtualFileId) -> Option<&PathBuf> { - self.data.path_by_id(vfid) - } -} diff --git a/data/src/data/local/modified_status.rs b/data/src/data/local/modified_status.rs deleted file mode 100644 index e0e6dd5..0000000 --- a/data/src/data/local/modified_status.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::{constants::CLIENT_FILE_VAULT_MODIFIED, env::current_local_path}; - -pub async fn check_vault_modified() -> bool { - let Some(current_dir) = current_local_path() else { - return false; - }; - - let record_file = current_dir.join(CLIENT_FILE_VAULT_MODIFIED); - if !record_file.exists() { - return false; - } - - let Ok(contents) = tokio::fs::read_to_string(&record_file).await else { - return false; - }; - - matches!(contents.trim().to_lowercase().as_str(), "true") -} - -pub async fn sign_vault_modified(modified: bool) { - let Some(current_dir) = current_local_path() else { - return; - }; - - let record_file = current_dir.join(CLIENT_FILE_VAULT_MODIFIED); - - let contents = if modified { "true" } else { "false" }; - - let _ = tokio::fs::write(&record_file, contents).await; -} diff --git a/data/src/data/local/workspace_analyzer.rs b/data/src/data/local/workspace_analyzer.rs deleted file mode 100644 index 5d73e03..0000000 --- a/data/src/data/local/workspace_analyzer.rs +++ /dev/null @@ -1,359 +0,0 @@ -use std::{ - collections::{HashMap, HashSet}, - io::Error, - path::PathBuf, -}; - -use serde::Serialize; -use sha1_hash::calc_sha1_multi; -use string_proc::format_path::format_path; -use walkdir::WalkDir; - -use crate::data::{ - local::{LocalWorkspace, cached_sheet::CachedSheet, local_sheet::LocalSheet}, - member::MemberId, - sheet::{SheetData, SheetName}, - vault::virtual_file::VirtualFileId, -}; - -pub type FromRelativePathBuf = PathBuf; -pub type ToRelativePathBuf = PathBuf; -pub type CreatedRelativePathBuf = PathBuf; -pub type LostRelativePathBuf = PathBuf; -pub type ModifiedRelativePathBuf = PathBuf; - -pub struct AnalyzeResult<'a> { - local_workspace: &'a LocalWorkspace, - - /// Moved local files - pub moved: HashMap, - - /// Newly created local files - pub created: HashSet, - - /// Lost local files - pub lost: HashSet, - - /// Erased local files - pub erased: HashSet, - - /// Modified local files (excluding moved files) - /// For files that were both moved and modified, changes can only be detected after LocalSheet mapping is aligned with actual files - pub modified: HashSet, -} - -#[derive(Serialize, Default)] -pub struct AnalyzeResultPure { - /// Moved local files - pub moved: HashMap, - - /// Newly created local files - pub created: HashSet, - - /// Lost local files - pub lost: HashSet, - - /// Erased local files - pub erased: HashSet, - - /// Modified local files (excluding moved files) - /// For files that were both moved and modified, changes can only be detected after LocalSheet mapping is aligned with actual files - pub modified: HashSet, -} - -impl<'a> From> for AnalyzeResultPure { - fn from(result: AnalyzeResult<'a>) -> Self { - AnalyzeResultPure { - moved: result.moved, - created: result.created, - lost: result.lost, - erased: result.erased, - modified: result.modified, - } - } -} - -struct AnalyzeContext<'a> { - member: MemberId, - sheet_name: SheetName, - local_sheet: Option>, - cached_sheet_data: Option, -} - -impl<'a> AnalyzeResult<'a> { - /// Analyze all files, calculate the file information provided - pub async fn analyze_local_status( - local_workspace: &'a LocalWorkspace, - ) -> Result, std::io::Error> { - // Workspace - let workspace = local_workspace; - - // Current member, sheet - let (member, sheet_name) = { - let mut_workspace = workspace.config.lock().await; - let member = mut_workspace.current_account(); - let Some(sheet) = mut_workspace.sheet_in_use().clone() else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Sheet not found")); - }; - (member, sheet) - }; - - // Local files (RelativePaths) - let local_path = workspace.local_path(); - let file_relative_paths = { - let mut paths = HashSet::new(); - for entry in WalkDir::new(local_path) { - let entry = match entry { - Ok(entry) => entry, - Err(_) => continue, - }; - - // Skip entries that contain ".jv" in their path - if entry.path().to_string_lossy().contains(".jv") { - continue; - } - - if entry.file_type().is_file() - && let Ok(relative_path) = entry.path().strip_prefix(local_path) - { - let format = format_path(relative_path.to_path_buf()); - let Ok(format) = format else { - continue; - }; - paths.insert(format); - } - } - - paths - }; - - // Read local sheet - let local_sheet = (workspace.local_sheet(&member, &sheet_name).await).ok(); - - // Read cached sheet - let cached_sheet_data = match CachedSheet::cached_sheet_data(&sheet_name).await { - Ok(v) => Some(v), - Err(_) => { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "Cached sheet not found", - )); - } - }; - - // Create new result - let mut result = Self::none_result(workspace); - - // Analyze entry - let mut analyze_ctx = AnalyzeContext { - member, - sheet_name, - local_sheet, - cached_sheet_data, - }; - Self::analyze_moved(&mut result, &file_relative_paths, &analyze_ctx, workspace).await?; - Self::analyze_modified( - &mut result, - &file_relative_paths, - &mut analyze_ctx, - workspace, - ) - .await?; - - Ok(result) - } - - /// Track file moves by comparing recorded SHA1 hashes with actual file SHA1 hashes - /// For files that cannot be directly matched, continue searching using fuzzy matching algorithms - async fn analyze_moved( - result: &mut AnalyzeResult<'_>, - file_relative_paths: &HashSet, - analyze_ctx: &AnalyzeContext<'a>, - workspace: &LocalWorkspace, - ) -> Result<(), std::io::Error> { - let local_sheet_paths: HashSet<&PathBuf> = match &analyze_ctx.local_sheet { - Some(local_sheet) => local_sheet.data.mapping.keys().collect(), - None => HashSet::new(), - }; - let file_relative_paths_ref: HashSet<&PathBuf> = file_relative_paths.iter().collect(); - - // Files that exist locally but not in remote - let mut erased_files: HashSet = HashSet::new(); - - if let Some(cached_data) = &analyze_ctx.cached_sheet_data { - if let Some(local_sheet) = &analyze_ctx.local_sheet { - let cached_sheet_mapping = cached_data.mapping(); - let local_sheet_mapping = &local_sheet.data.mapping; - - // Find paths that exist in local sheet but not in cached sheet - for local_path in local_sheet_mapping.keys() { - if !cached_sheet_mapping.contains_key(local_path) { - erased_files.insert(local_path.clone()); - } - } - } - } - - // Files that exist in the local sheet but not in reality are considered lost - let mut lost_files: HashSet<&PathBuf> = local_sheet_paths - .difference(&file_relative_paths_ref) - .filter(|&&path| !erased_files.contains(path)) - .cloned() - .collect(); - - // Files that exist in reality but not in the local sheet are recorded as newly created - let mut new_files: HashSet<&PathBuf> = file_relative_paths_ref - .difference(&local_sheet_paths) - .cloned() - .collect(); - - // Calculate hashes for new files - let new_files_for_hash: Vec = new_files - .iter() - .map(|p| workspace.local_path.join(p)) - .collect(); - let file_hashes: HashSet<(PathBuf, String)> = - match calc_sha1_multi::>(new_files_for_hash, 8192).await { - Ok(hash) => hash, - Err(e) => return Err(Error::other(e)), - } - .iter() - .map(|r| (r.file_path.clone(), r.hash.to_string())) - .collect(); - - // Build hash mapping table for lost files - let mut lost_files_hash_mapping: HashMap = - match &analyze_ctx.local_sheet { - Some(local_sheet) => lost_files - .iter() - .filter_map(|f| { - local_sheet.mapping_data(f).ok().map(|mapping_data| { - ( - // Using the most recently recorded Hash can more accurately identify moved items, - // but if it doesn't exist, fall back to the initially recorded Hash - mapping_data - .last_modify_check_hash - .as_ref() - .cloned() - .unwrap_or(mapping_data.hash_when_updated.clone()), - (*f).clone(), - ) - }) - }) - .collect(), - None => HashMap::new(), - }; - - // If these hashes correspond to the hashes of missing files, then this pair of new and lost items will be merged into moved items - let mut moved_files: HashSet<(FromRelativePathBuf, ToRelativePathBuf)> = HashSet::new(); - for (new_path, new_hash) in file_hashes { - let new_path = new_path - .strip_prefix(&workspace.local_path) - .map(|p| p.to_path_buf()) - .unwrap_or(new_path); - - // If the new hash value hits the mapping, add a moved item - if let Some(lost_path) = lost_files_hash_mapping.remove(&new_hash) { - // Remove this new item and lost item - lost_files.remove(&lost_path); - new_files.remove(&new_path); - - // Create moved item - moved_files.insert((lost_path.clone(), new_path)); - } - } - - // Enter fuzzy matching to match other potentially moved items that haven't been matched - // If the total number of new and lost files is divisible by 2, it indicates there might still be files that have been moved, consider trying fuzzy matching - if new_files.len() + lost_files.len() % 2 == 0 { - // Try fuzzy matching - // ... - } - - // Collect results and set the result - result.created = new_files.iter().map(|p| (*p).clone()).collect(); - result.lost = lost_files.iter().map(|p| (*p).clone()).collect(); - result.moved = moved_files - .iter() - .filter_map(|(from, to)| { - let vfid = analyze_ctx - .local_sheet - .as_ref() - .and_then(|local_sheet| local_sheet.mapping_data(from).ok()) - .map(|mapping_data| mapping_data.mapping_vfid.clone()); - vfid.map(|vfid| (vfid, (from.clone(), to.clone()))) - }) - .collect(); - result.erased = erased_files; - - Ok(()) - } - - /// Compare using file modification time and SHA1 hash values. - /// Note: For files that have been both moved and modified, they can only be recognized as modified after their location is matched. - async fn analyze_modified( - result: &mut AnalyzeResult<'_>, - file_relative_paths: &HashSet, - analyze_ctx: &mut AnalyzeContext<'a>, - workspace: &LocalWorkspace, - ) -> Result<(), std::io::Error> { - let local_sheet = &mut analyze_ctx.local_sheet.as_mut().unwrap(); - let local_path = local_sheet.local_workspace.local_path().clone(); - - for path in file_relative_paths { - // Get mapping data - let Ok(mapping_data) = local_sheet.mapping_data_mut(path) else { - continue; - }; - - // If modified time not changed, skip - let modified_time = std::fs::metadata(local_path.join(path))?.modified()?; - if &modified_time == mapping_data.last_modifiy_check_time() { - if mapping_data.last_modifiy_check_result() { - result.modified.insert(path.clone()); - } - continue; - } - - // Calculate hash - let hash_calc = match sha1_hash::calc_sha1(workspace.local_path.join(path), 2048).await - { - Ok(hash) => hash, - Err(e) => return Err(Error::other(e)), - }; - - // If hash not match, mark as modified - if &hash_calc.hash != mapping_data.hash_when_updated() { - result.modified.insert(path.clone()); - - // Update last modified check time to modified time - mapping_data.last_modify_check_time = modified_time; - mapping_data.last_modify_check_result = true; - } else { - // Update last modified check time to modified time - mapping_data.last_modify_check_time = modified_time; - mapping_data.last_modify_check_result = false; - } - - // Record latest hash - mapping_data.last_modify_check_hash = Some(hash_calc.hash) - } - - // Persist the local sheet data - LocalSheet::write(local_sheet).await?; - - Ok(()) - } - - /// Generate a empty AnalyzeResult - fn none_result(local_workspace: &'a LocalWorkspace) -> AnalyzeResult<'a> { - AnalyzeResult { - local_workspace, - moved: HashMap::new(), - created: HashSet::new(), - lost: HashSet::new(), - modified: HashSet::new(), - erased: HashSet::new(), - } - } -} diff --git a/data/src/data/local/workspace_config.rs b/data/src/data/local/workspace_config.rs deleted file mode 100644 index f97d049..0000000 --- a/data/src/data/local/workspace_config.rs +++ /dev/null @@ -1,374 +0,0 @@ -use cfg_file::ConfigFile; -use cfg_file::config::ConfigFile; -use serde::{Deserialize, Serialize}; -use std::io::Error; -use std::net::SocketAddr; -use std::path::Path; -use std::path::PathBuf; -use string_proc::snake_case; - -use crate::constants::CLIENT_FILE_WORKSPACE; -use crate::constants::CLIENT_FOLDER_WORKSPACE_ROOT_NAME; -use crate::constants::CLIENT_PATH_LOCAL_DRAFT; -use crate::constants::CLIENT_PATH_WORKSPACE_ROOT; -use crate::constants::KEY_ACCOUNT; -use crate::constants::KEY_SHEET_NAME; -use crate::constants::PORT; -use crate::data::local::latest_info::LatestInfo; -use crate::data::member::MemberId; -use crate::data::sheet::SheetName; -use crate::data::vault::vault_config::VaultUuid; -use crate::env::current_local_path; - -#[derive(Serialize, Deserialize, ConfigFile, Clone)] -#[cfg_file(path = CLIENT_FILE_WORKSPACE)] -pub struct LocalConfig { - /// The upstream address, representing the upstream address of the local workspace, - /// to facilitate timely retrieval of new updates from the upstream source. - #[serde(rename = "addr")] - upstream_addr: SocketAddr, - - /// The member ID used by the current local workspace. - /// This ID will be used to verify access permissions when connecting to the upstream server. - #[serde(rename = "as")] - using_account: MemberId, - - /// Whether the current member is interacting as a host. - /// In host mode, full Vault operation permissions are available except for adding new content. - #[serde(rename = "host")] - using_host_mode: bool, - - /// Whether the local workspace is stained. - /// - /// If stained, it can only set an upstream server with the same identifier. - /// - /// If the value is None, it means not stained; - /// otherwise, it contains the stain identifier (i.e., the upstream vault's unique ID) - #[serde(rename = "up_uid")] - stained_uuid: Option, - - /// The name of the sheet currently in use. - #[serde(rename = "use")] - sheet_in_use: Option, -} - -impl Default for LocalConfig { - fn default() -> Self { - Self { - upstream_addr: SocketAddr::V4(std::net::SocketAddrV4::new( - std::net::Ipv4Addr::new(127, 0, 0, 1), - PORT, - )), - using_account: "unknown".to_string(), - using_host_mode: false, - stained_uuid: None, - sheet_in_use: None, - } - } -} - -impl LocalConfig { - /// Set the vault address. - pub fn set_vault_addr(&mut self, addr: SocketAddr) { - self.upstream_addr = addr; - } - - /// Get the vault address. - pub fn vault_addr(&self) -> SocketAddr { - self.upstream_addr - } - - /// Set the currently used account - pub fn set_current_account(&mut self, account: MemberId) -> Result<(), std::io::Error> { - if self.sheet_in_use().is_some() { - return Err(Error::new( - std::io::ErrorKind::DirectoryNotEmpty, - "Please exit the current sheet before switching accounts", - )); - } - self.using_account = account; - Ok(()) - } - - /// Set the host mode - pub fn set_host_mode(&mut self, host_mode: bool) { - self.using_host_mode = host_mode; - } - - /// Set the currently used sheet - pub async fn use_sheet(&mut self, sheet: SheetName) -> Result<(), std::io::Error> { - let sheet = snake_case!(sheet); - - // Check if the sheet is already in use - if self.sheet_in_use().is_some() { - return Err(std::io::Error::new( - std::io::ErrorKind::AlreadyExists, - "Sheet already in use", - )); - }; - - // Check if the local path exists - let local_path = self.get_local_path().await?; - - // Get latest info - let Ok(latest_info) = LatestInfo::read_from(LatestInfo::latest_info_path( - &local_path, - &self.current_account(), - )) - .await - else { - return Err(std::io::Error::new( - std::io::ErrorKind::NotFound, - "No latest info found", - )); - }; - - // Check if the sheet exists - if !latest_info.visible_sheets.contains(&sheet) { - return Err(std::io::Error::new( - std::io::ErrorKind::NotFound, - "Sheet not found", - )); - } - - // Check if there are any files or folders other than .jv - self.check_local_path_empty(&local_path).await?; - - // Get the draft folder path - let draft_folder = self.draft_folder(&self.using_account, &sheet, &local_path); - - if draft_folder.exists() { - // Exists - // Move the contents of the draft folder to the local path with rollback support - self.move_draft_to_local(&draft_folder, &local_path).await?; - } - - self.sheet_in_use = Some(sheet); - LocalConfig::write(self).await?; - - Ok(()) - } - - /// Exit the currently used sheet - pub async fn exit_sheet(&mut self) -> Result<(), std::io::Error> { - // Check if the sheet is already in use - if self.sheet_in_use().is_none() { - return Ok(()); - } - - // Check if the local path exists - let local_path = self.get_local_path().await?; - - // Get the current sheet name - let sheet_name = self.sheet_in_use().as_ref().unwrap().clone(); - - // Get the draft folder path - let draft_folder = self.draft_folder(&self.using_account, &sheet_name, &local_path); - - // Create the draft folder if it doesn't exist - if !draft_folder.exists() { - std::fs::create_dir_all(&draft_folder).map_err(std::io::Error::other)?; - } - - // Move all files and folders (except .jv folder) to the draft folder with rollback support - self.move_local_to_draft(&local_path, &draft_folder).await?; - - // Clear the sheet in use - self.sheet_in_use = None; - LocalConfig::write(self).await?; - - Ok(()) - } - - /// Get local path or return error - async fn get_local_path(&self) -> Result { - current_local_path().ok_or_else(|| { - std::io::Error::new(std::io::ErrorKind::NotFound, "Fail to get local path") - }) - } - - /// Check if local path is empty (except for .jv folder) - async fn check_local_path_empty(&self, local_path: &Path) -> Result<(), std::io::Error> { - let jv_folder = local_path.join(CLIENT_PATH_WORKSPACE_ROOT); - let mut entries = std::fs::read_dir(local_path).map_err(std::io::Error::other)?; - - if entries.any(|entry| { - if let Ok(entry) = entry { - let path = entry.path(); - path != jv_folder - && path.file_name().and_then(|s| s.to_str()) - != Some(CLIENT_FOLDER_WORKSPACE_ROOT_NAME) - } else { - false - } - }) { - return Err(std::io::Error::new( - std::io::ErrorKind::DirectoryNotEmpty, - "Local path is not empty!", - )); - } - - Ok(()) - } - - /// Move contents from draft folder to local path with rollback support - async fn move_draft_to_local( - &self, - draft_folder: &Path, - local_path: &Path, - ) -> Result<(), std::io::Error> { - let draft_entries: Vec<_> = std::fs::read_dir(draft_folder) - .map_err(std::io::Error::other)? - .collect::, _>>() - .map_err(std::io::Error::other)?; - - let mut moved_items: Vec = Vec::new(); - - for entry in &draft_entries { - let entry_path = entry.path(); - let target_path = local_path.join(entry_path.file_name().unwrap()); - - // Move each file/directory from draft folder to local path - std::fs::rename(&entry_path, &target_path).map_err(|e| { - // Rollback all previously moved items - for moved_item in &moved_items { - let _ = std::fs::rename(&moved_item.target, &moved_item.source); - } - std::io::Error::other(e) - })?; - - moved_items.push(MovedItem { - source: entry_path.clone(), - target: target_path.clone(), - }); - } - - // Remove the now-empty draft folder - std::fs::remove_dir(draft_folder).map_err(|e| { - // Rollback all moved items if folder removal fails - for moved_item in &moved_items { - let _ = std::fs::rename(&moved_item.target, &moved_item.source); - } - std::io::Error::other(e) - })?; - - Ok(()) - } - - /// Move contents from local path to draft folder with rollback support (except .jv folder) - async fn move_local_to_draft( - &self, - local_path: &Path, - draft_folder: &Path, - ) -> Result<(), std::io::Error> { - let jv_folder = local_path.join(CLIENT_PATH_WORKSPACE_ROOT); - let entries: Vec<_> = std::fs::read_dir(local_path) - .map_err(std::io::Error::other)? - .collect::, _>>() - .map_err(std::io::Error::other)?; - - let mut moved_items: Vec = Vec::new(); - - for entry in &entries { - let entry_path = entry.path(); - - // Skip the .jv folder - if entry_path == jv_folder - || entry_path.file_name().and_then(|s| s.to_str()) - == Some(CLIENT_FOLDER_WORKSPACE_ROOT_NAME) - { - continue; - } - - let target_path = draft_folder.join(entry_path.file_name().unwrap()); - - // Move each file/directory from local path to draft folder - std::fs::rename(&entry_path, &target_path).map_err(|e| { - // Rollback all previously moved items - for moved_item in &moved_items { - let _ = std::fs::rename(&moved_item.target, &moved_item.source); - } - std::io::Error::other(e) - })?; - - moved_items.push(MovedItem { - source: entry_path.clone(), - target: target_path.clone(), - }); - } - - Ok(()) - } - - /// Get the currently used account - pub fn current_account(&self) -> MemberId { - self.using_account.clone() - } - - /// Check if the current member is interacting as a host. - pub fn is_host_mode(&self) -> bool { - self.using_host_mode - } - - /// Check if the local workspace is stained. - pub fn stained(&self) -> bool { - self.stained_uuid.is_some() - } - - /// Get the UUID of the vault that the local workspace is stained with. - pub fn stained_uuid(&self) -> Option { - self.stained_uuid - } - - /// Stain the local workspace with the given UUID. - pub fn stain(&mut self, uuid: VaultUuid) { - self.stained_uuid = Some(uuid); - } - - /// Unstain the local workspace. - pub fn unstain(&mut self) { - self.stained_uuid = None; - } - - /// Get the upstream address. - pub fn upstream_addr(&self) -> SocketAddr { - self.upstream_addr - } - - /// Get the currently used sheet - pub fn sheet_in_use(&self) -> &Option { - &self.sheet_in_use - } - - /// Get draft folder - pub fn draft_folder( - &self, - account: &MemberId, - sheet_name: &SheetName, - local_workspace_path: impl Into, - ) -> PathBuf { - let account_str = snake_case!(account.as_str()); - let sheet_name_str = snake_case!(sheet_name.as_str()); - let draft_path = CLIENT_PATH_LOCAL_DRAFT - .replace(KEY_ACCOUNT, &account_str) - .replace(KEY_SHEET_NAME, &sheet_name_str); - local_workspace_path.into().join(draft_path) - } - - /// Get current draft folder - pub fn current_draft_folder(&self) -> Option { - let Some(sheet_name) = self.sheet_in_use() else { - return None; - }; - - let current_dir = current_local_path()?; - - Some(self.draft_folder(&self.using_account, sheet_name, current_dir)) - } -} - -#[derive(Clone)] -struct MovedItem { - source: PathBuf, - target: PathBuf, -} diff --git a/data/src/data/member.rs b/data/src/data/member.rs deleted file mode 100644 index 7e99488..0000000 --- a/data/src/data/member.rs +++ /dev/null @@ -1,71 +0,0 @@ -use std::collections::HashMap; - -use cfg_file::ConfigFile; -use serde::{Deserialize, Serialize}; -use string_proc::snake_case; - -pub type MemberId = String; - -#[derive(Debug, Eq, Clone, ConfigFile, Serialize, Deserialize)] -pub struct Member { - /// Member ID, the unique identifier of the member - #[serde(rename = "id")] - id: String, - - /// Member metadata - #[serde(rename = "meta")] - metadata: HashMap, -} - -impl Default for Member { - fn default() -> Self { - Self::new("default_user") - } -} - -impl PartialEq for Member { - fn eq(&self, other: &Self) -> bool { - self.id == other.id - } -} - -impl std::fmt::Display for Member { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.id) - } -} - -impl std::convert::AsRef for Member { - fn as_ref(&self) -> &str { - &self.id - } -} - -impl Member { - /// Create member struct by id - pub fn new(new_id: impl Into) -> Self { - Self { - id: snake_case!(new_id.into()), - metadata: HashMap::new(), - } - } - - /// Get member id - pub fn id(&self) -> String { - self.id.clone() - } - - /// Get metadata - pub fn metadata(&self, key: impl Into) -> Option<&String> { - self.metadata.get(&key.into()) - } - - /// Set metadata - pub fn set_metadata( - &mut self, - key: impl AsRef, - value: impl Into, - ) -> Option { - self.metadata.insert(key.as_ref().to_string(), value.into()) - } -} diff --git a/data/src/data/sheet.rs b/data/src/data/sheet.rs deleted file mode 100644 index 8b427e9..0000000 --- a/data/src/data/sheet.rs +++ /dev/null @@ -1,278 +0,0 @@ -use std::{collections::HashMap, path::PathBuf}; - -use cfg_file::{ConfigFile, config::ConfigFile}; -use serde::{Deserialize, Serialize}; - -use crate::{ - constants::{KEY_SHEET_NAME, SERVER_FILE_SHEET}, - data::{ - member::MemberId, - vault::{ - Vault, - virtual_file::{VirtualFileId, VirtualFileVersion}, - }, - }, -}; - -pub type SheetName = String; -pub type SheetPathBuf = PathBuf; - -pub struct Sheet<'a> { - /// The name of the current sheet - pub(crate) name: SheetName, - - /// Sheet data - pub(crate) data: SheetData, - - /// Sheet path - pub(crate) vault_reference: &'a Vault, -} - -#[derive(Default, Serialize, Deserialize, ConfigFile, Clone)] -pub struct SheetData { - /// The write count of the current sheet - #[serde(rename = "v")] - pub(crate) write_count: i32, - - /// The holder of the current sheet, who has full operation rights to the sheet mapping - #[serde(rename = "holder")] - pub(crate) holder: Option, - - /// Mapping of sheet paths to virtual file IDs - #[serde(rename = "map")] - pub(crate) mapping: HashMap, - - /// Mapping of virtual file Ids to sheet paths - #[serde(rename = "id_map")] - pub(crate) id_mapping: Option>, -} - -#[derive(Debug, Default, Serialize, Deserialize, ConfigFile, Clone, Eq, PartialEq)] -pub struct SheetMappingMetadata { - #[serde(rename = "id")] - pub id: VirtualFileId, - #[serde(rename = "ver")] - pub version: VirtualFileVersion, -} - -impl<'a> Sheet<'a> { - pub fn name(&self) -> &SheetName { - &self.name - } - - /// Get the holder of this sheet - pub fn holder(&self) -> Option<&MemberId> { - self.data.holder.as_ref() - } - - /// Get the mapping of this sheet - pub fn mapping(&self) -> &HashMap { - &self.data.mapping - } - - /// Get the muttable mapping of this sheet - pub fn mapping_mut(&mut self) -> &mut HashMap { - &mut self.data.mapping - } - - /// Get the id_mapping of this sheet data - pub fn id_mapping(&self) -> &Option> { - &self.data.id_mapping - } - - /// Get the write count of this sheet - pub fn write_count(&self) -> i32 { - self.data.write_count - } - - /// Forget the holder of this sheet - pub fn forget_holder(&mut self) { - self.data.holder = None; - } - - /// Set the holder of this sheet - pub fn set_holder(&mut self, holder: MemberId) { - self.data.holder = Some(holder); - } - - /// Add (or Edit) a mapping entry to the sheet - /// - /// This operation performs safety checks to ensure the member has the right to add the mapping: - /// 1. The sheet must have a holder (member) to perform this operation - /// 2. If the virtual file ID doesn't exist in the vault, the mapping is added directly - /// 3. If the virtual file exists, the mapping is added regardless of member edit rights - /// - /// Note: Full validation adds overhead - avoid frequent calls - pub async fn add_mapping( - &mut self, - sheet_path: SheetPathBuf, - virtual_file_id: VirtualFileId, - version: VirtualFileVersion, - ) -> Result<(), std::io::Error> { - // Check if the virtual file exists in the vault - if self.vault_reference.virtual_file(&virtual_file_id).is_err() { - // Virtual file doesn't exist, add the mapping directly - self.data.mapping.insert( - sheet_path, - SheetMappingMetadata { - id: virtual_file_id, - version, - }, - ); - return Ok(()); - } - - // Check if the sheet has a holder - let Some(_) = self.holder() else { - return Err(std::io::Error::new( - std::io::ErrorKind::PermissionDenied, - "This sheet has no holder", - )); - }; - - self.data.mapping.insert( - sheet_path, - SheetMappingMetadata { - id: virtual_file_id, - version, - }, - ); - - Ok(()) - } - - /// Remove a mapping entry from the sheet - /// - /// This operation performs safety checks to ensure the member has the right to remove the mapping: - /// 1. The sheet must have a holder (member) to perform this operation - /// 2. Member must NOT have edit rights to the virtual file to release it (ensuring clear ownership) - /// 3. If the virtual file doesn't exist, the mapping is removed but no ID is returned - /// 4. If member has no edit rights and the file exists, returns the removed virtual file ID - /// - /// Note: Full validation adds overhead - avoid frequent calls - pub async fn remove_mapping( - &mut self, - sheet_path: &SheetPathBuf, - ) -> Option { - let virtual_file_meta = match self.data.mapping.get(sheet_path) { - Some(id) => id, - None => { - // The mapping entry doesn't exist, nothing to remove - return None; - } - }; - - // Check if the virtual file exists in the vault - if self - .vault_reference - .virtual_file(&virtual_file_meta.id) - .is_err() - { - // Virtual file doesn't exist, remove the mapping and return None - self.data.mapping.remove(sheet_path); - return None; - } - - // Check if the sheet has a holder - let holder = self.holder()?; - - // Check if the holder has edit rights to the virtual file - match self - .vault_reference - .has_virtual_file_edit_right(holder, &virtual_file_meta.id) - .await - { - Ok(false) => { - // Holder doesn't have rights, remove and return the virtual file ID - self.data.mapping.remove(sheet_path) - } - Ok(true) => { - // Holder has edit rights, don't remove the mapping - None - } - Err(_) => { - // Error checking rights, don't remove the mapping - None - } - } - } - - /// Persist the sheet to disk - /// - /// Why not use a reference? - /// Because I don't want a second instance of the sheet to be kept in memory. - /// If needed, please deserialize and reload it. - pub async fn persist(mut self) -> Result<(), std::io::Error> { - self.data.write_count += 1; - - // Update id mapping - self.data.id_mapping = Some(HashMap::new()); - for map in self.data.mapping.iter() { - self.data - .id_mapping - .as_mut() - .unwrap() - .insert(map.1.id.clone(), map.0.clone()); - } - - // Add write count - if self.data.write_count >= i32::MAX - 1 { - self.data.write_count = 0; - } - SheetData::write_to(&self.data, self.sheet_path()).await - } - - /// Get the path to the sheet file - pub fn sheet_path(&self) -> PathBuf { - Sheet::sheet_path_with_name(self.vault_reference, &self.name) - } - - /// Get the path to the sheet file with the given name - pub fn sheet_path_with_name(vault: &Vault, name: impl AsRef) -> PathBuf { - vault - .vault_path() - .join(SERVER_FILE_SHEET.replace(KEY_SHEET_NAME, name.as_ref())) - } - - /// Clone the data of the sheet - pub fn clone_data(&self) -> SheetData { - self.data.clone() - } - - /// Convert the sheet into its data representation - pub fn to_data(self) -> SheetData { - self.data - } -} - -impl SheetData { - /// Get the write count of this sheet data - pub fn write_count(&self) -> i32 { - self.write_count - } - - /// Get the holder of this sheet data - pub fn holder(&self) -> Option<&MemberId> { - self.holder.as_ref() - } - - /// Get the mapping of this sheet data - pub fn mapping(&self) -> &HashMap { - &self.mapping - } - - /// Get the muttable mapping of this sheet data - pub fn mapping_mut(&mut self) -> &mut HashMap { - &mut self.mapping - } - - /// Get the id_mapping of this sheet data - pub fn id_mapping(&self) -> &Option> { - &self.id_mapping - } - - /// Get the muttable id_mapping of this sheet data - pub fn id_mapping_mut(&mut self) -> &mut Option> { - &mut self.id_mapping - } -} diff --git a/data/src/data/user.rs b/data/src/data/user.rs deleted file mode 100644 index a2326fa..0000000 --- a/data/src/data/user.rs +++ /dev/null @@ -1,28 +0,0 @@ -use crate::env::current_cfg_dir; -use std::path::PathBuf; - -pub mod accounts; - -pub struct UserDirectory { - local_path: PathBuf, -} - -impl UserDirectory { - /// Create a user ditectory struct from the current system's document directory - pub fn current_cfg_dir() -> Option { - Some(UserDirectory { - local_path: current_cfg_dir()?, - }) - } - - /// Create a user directory struct from a specified directory path - /// Returns None if the directory does not exist - pub fn from_path>(path: P) -> Option { - let local_path = path.into(); - if local_path.exists() { - Some(UserDirectory { local_path }) - } else { - None - } - } -} diff --git a/data/src/data/user/accounts.rs b/data/src/data/user/accounts.rs deleted file mode 100644 index def2677..0000000 --- a/data/src/data/user/accounts.rs +++ /dev/null @@ -1,162 +0,0 @@ -use std::{ - fs, - io::{Error, ErrorKind}, - path::PathBuf, -}; - -use cfg_file::config::ConfigFile; - -use crate::{ - constants::{KEY_SELF_ID, USER_FILE_ACCOUNTS, USER_FILE_KEY, USER_FILE_MEMBER}, - data::{ - member::{Member, MemberId}, - user::UserDirectory, - }, -}; - -/// Account Management -impl UserDirectory { - /// Read account from configuration file - pub async fn account(&self, id: &MemberId) -> Result { - if let Some(cfg_file) = self.account_cfg(id) { - let member = Member::read_from(cfg_file).await?; - return Ok(member); - } - - Err(Error::new(ErrorKind::NotFound, "Account not found!")) - } - - /// List all account IDs in the user directory - pub fn account_ids(&self) -> Result, std::io::Error> { - let accounts_path = self - .local_path - .join(USER_FILE_ACCOUNTS.replace(KEY_SELF_ID, "")); - - if !accounts_path.exists() { - return Ok(Vec::new()); - } - - let mut account_ids = Vec::new(); - - for entry in fs::read_dir(accounts_path)? { - let entry = entry?; - let path = entry.path(); - - if path.is_file() - && let Some(file_name) = path.file_stem().and_then(|s| s.to_str()) - && path.extension().and_then(|s| s.to_str()) == Some("toml") - { - // Remove the "_private" suffix from key files if present - let account_id = file_name.replace("_private", ""); - account_ids.push(account_id); - } - } - - Ok(account_ids) - } - - /// Get all accounts - /// This method will read and deserialize account information, please pay attention to performance issues - pub async fn accounts(&self) -> Result, std::io::Error> { - let mut accounts = Vec::new(); - - for account_id in self.account_ids()? { - if let Ok(account) = self.account(&account_id).await { - accounts.push(account); - } - } - - Ok(accounts) - } - - /// Update account info - pub async fn update_account(&self, member: Member) -> Result<(), std::io::Error> { - // Ensure account exist - if self.account_cfg(&member.id()).is_some() { - let account_cfg_path = self.account_cfg_path(&member.id()); - Member::write_to(&member, account_cfg_path).await?; - return Ok(()); - } - - Err(Error::new(ErrorKind::NotFound, "Account not found!")) - } - - /// Register an account to user directory - pub async fn register_account(&self, member: Member) -> Result<(), std::io::Error> { - // Ensure account not exist - if self.account_cfg(&member.id()).is_some() { - return Err(Error::new( - ErrorKind::DirectoryNotEmpty, - format!("Account `{}` already registered!", member.id()), - )); - } - - // Ensure accounts directory exists - let accounts_dir = self - .local_path - .join(USER_FILE_ACCOUNTS.replace(KEY_SELF_ID, "")); - if !accounts_dir.exists() { - fs::create_dir_all(&accounts_dir)?; - } - - // Write config file to accounts dir - let account_cfg_path = self.account_cfg_path(&member.id()); - Member::write_to(&member, account_cfg_path).await?; - - Ok(()) - } - - /// Remove account from user directory - pub fn remove_account(&self, id: &MemberId) -> Result<(), std::io::Error> { - // Remove config file if exists - if let Some(account_cfg_path) = self.account_cfg(id) { - fs::remove_file(account_cfg_path)?; - } - - // Remove private key file if exists - if let Some(private_key_path) = self.account_private_key(id) - && private_key_path.exists() - { - fs::remove_file(private_key_path)?; - } - - Ok(()) - } - - /// Try to get the account's configuration file to determine if the account exists - pub fn account_cfg(&self, id: &MemberId) -> Option { - let cfg_file = self.account_cfg_path(id); - if cfg_file.exists() { - Some(cfg_file) - } else { - None - } - } - - /// Try to get the account's private key file to determine if the account has a private key - pub fn account_private_key(&self, id: &MemberId) -> Option { - let key_file = self.account_private_key_path(id); - if key_file.exists() { - Some(key_file) - } else { - None - } - } - - /// Check if account has private key - pub fn has_private_key(&self, id: &MemberId) -> bool { - self.account_private_key(id).is_some() - } - - /// Get the account's configuration file path, but do not check if the file exists - pub fn account_cfg_path(&self, id: &MemberId) -> PathBuf { - self.local_path - .join(USER_FILE_MEMBER.replace(KEY_SELF_ID, id.to_string().as_str())) - } - - /// Get the account's private key file path, but do not check if the file exists - pub fn account_private_key_path(&self, id: &MemberId) -> PathBuf { - self.local_path - .join(USER_FILE_KEY.replace(KEY_SELF_ID, id.to_string().as_str())) - } -} diff --git a/data/src/data/vault.rs b/data/src/data/vault.rs deleted file mode 100644 index 0f93016..0000000 --- a/data/src/data/vault.rs +++ /dev/null @@ -1,132 +0,0 @@ -use std::{env::current_dir, path::PathBuf, sync::Arc}; - -use tokio::fs::create_dir_all; -use vcs_docs::docs::READMES_VAULT_README; - -use crate::{ - constants::{ - REF_SHEET_NAME, SERVER_FILE_README, SERVER_FILE_VAULT, SERVER_PATH_MEMBER_PUB, - SERVER_PATH_MEMBERS, SERVER_PATH_SHEETS, SERVER_PATH_VF_ROOT, VAULT_HOST_NAME, - }, - data::{member::Member, vault::vault_config::VaultConfig}, - env::{current_vault_path, find_vault_path}, -}; - -pub mod lock_status; -pub mod mapping_share; -pub mod member_manage; -pub mod sheet_manage; -pub mod vault_config; -pub mod virtual_file; - -pub struct Vault { - config: Arc, - vault_path: PathBuf, -} - -impl Vault { - /// Get vault path - pub fn vault_path(&self) -> &PathBuf { - &self.vault_path - } - - /// Initialize vault - pub fn init(config: VaultConfig, vault_path: impl Into) -> Option { - let vault_path = find_vault_path(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: Arc::new(config), - vault_path, - }) - } - - /// Setup vault - pub async fn setup_vault( - vault_path: impl Into, - vault_name: impl AsRef, - ) -> Result<(), std::io::Error> { - let vault_path: PathBuf = vault_path.into(); - - // Ensure directory is empty - if vault_path.exists() && vault_path.read_dir()?.next().is_some() { - return Err(std::io::Error::new( - std::io::ErrorKind::DirectoryNotEmpty, - "DirectoryNotEmpty", - )); - } - - // 1. Setup main config - let config = VaultConfig::default(); - - // NOTE: - // Do not use the write_to method provided by the ConfigFile trait to store the Vault configuration file - // Instead, use the PROFILES_VAULT content provided by the Documents Repository for writing - - // VaultConfig::write_to(&config, vault_path.join(SERVER_FILE_VAULT)).await?; - let config_content = vcs_docs::docs::PROFILES_VAULT - .replace("{vault_name}", vault_name.as_ref()) - .replace("{user_name}", whoami::username().as_str()) - .replace( - "{date_format}", - chrono::Local::now() - .format("%Y-%m-%d %H:%M") - .to_string() - .as_str(), - ) - .replace("{vault_uuid}", &config.vault_uuid().to_string()); - tokio::fs::write(vault_path.join(SERVER_FILE_VAULT), config_content).await?; - - // 2. Setup sheets directory - create_dir_all(vault_path.join(SERVER_PATH_SHEETS)).await?; - - // 3. Setup key directory - create_dir_all(vault_path.join(SERVER_PATH_MEMBER_PUB)).await?; - - // 4. Setup member directory - create_dir_all(vault_path.join(SERVER_PATH_MEMBERS)).await?; - - // 5. Setup storage directory - create_dir_all(vault_path.join(SERVER_PATH_VF_ROOT)).await?; - - let Some(vault) = Vault::init(config, &vault_path) else { - return Err(std::io::Error::other("Failed to initialize vault")); - }; - - // 6. Create host member - vault - .register_member_to_vault(Member::new(VAULT_HOST_NAME)) - .await?; - - // 7. Setup reference sheet - vault - .create_sheet(&REF_SHEET_NAME.to_string(), &VAULT_HOST_NAME.to_string()) - .await?; - - // Final, generate README.md - let readme_content = READMES_VAULT_README; - tokio::fs::write(vault_path.join(SERVER_FILE_README), readme_content).await?; - - Ok(()) - } - - /// Setup vault in current directory - pub async fn setup_vault_current_dir( - vault_name: impl AsRef, - ) -> Result<(), std::io::Error> { - Self::setup_vault(current_dir()?, vault_name).await?; - Ok(()) - } - - /// Get vault configuration - pub fn config(&self) -> &Arc { - &self.config - } -} diff --git a/data/src/data/vault/lock_status.rs b/data/src/data/vault/lock_status.rs deleted file mode 100644 index 3f59c30..0000000 --- a/data/src/data/vault/lock_status.rs +++ /dev/null @@ -1,40 +0,0 @@ -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(()) - } -} diff --git a/data/src/data/vault/mapping_share.rs b/data/src/data/vault/mapping_share.rs deleted file mode 100644 index 5d27859..0000000 --- a/data/src/data/vault/mapping_share.rs +++ /dev/null @@ -1,422 +0,0 @@ -use std::{collections::HashMap, io::Error, path::PathBuf}; - -use cfg_file::{ConfigFile, config::ConfigFile}; -use rand::{Rng, rng}; -use serde::{Deserialize, Serialize}; -use string_proc::{format_path, snake_case}; -use tokio::fs; - -use crate::{ - constants::{ - KEY_SHARE_ID, KEY_SHEET_NAME, SERVER_FILE_SHEET_SHARE, SERVER_PATH_SHARES, - SERVER_SUFFIX_SHEET_SHARE_FILE_NO_DOT, - }, - data::{ - member::MemberId, - sheet::{Sheet, SheetMappingMetadata, SheetName, SheetPathBuf}, - vault::Vault, - }, -}; - -pub type SheetShareId = String; - -#[derive(Default, Serialize, Deserialize, ConfigFile, Clone, Debug)] -pub struct Share { - /// Sharer: the member who created this share item - #[serde(rename = "sharer")] - pub sharer: MemberId, - - /// Description of the share item - #[serde(rename = "desc")] - pub description: String, - - /// Metadata path - #[serde(skip)] - pub path: Option, - - /// From: which sheet the member exported the file from - #[serde(rename = "from")] - pub from_sheet: SheetName, - - /// Mappings: the sheet mappings contained in the share item - #[serde(rename = "map")] - pub mappings: HashMap, -} - -#[derive(Default, Serialize, Deserialize, ConfigFile, Clone, PartialEq, Eq)] -pub enum ShareMergeMode { - /// If a path or file already exists during merge, prioritize the incoming share - /// Path conflict: replace the mapping content at the local path with the incoming content - /// File conflict: delete the original file mapping and create a new one - Overwrite, - - /// If a path or file already exists during merge, skip overwriting this entry - Skip, - - /// Pre-check for conflicts, prohibit merging if any conflicts are found - #[default] - Safe, - - /// Reject all shares - RejectAll, -} - -#[derive(Default, Serialize, Deserialize, ConfigFile, Clone)] -pub struct ShareMergeConflict { - /// Duplicate mappings exist - pub duplicate_mapping: Vec, - - /// Duplicate files exist - pub duplicate_file: Vec, -} - -impl ShareMergeConflict { - /// Check if there are no conflicts - pub fn ok(&self) -> bool { - self.duplicate_mapping.is_empty() && self.duplicate_file.is_empty() - } -} - -impl Vault { - /// Get the path of a share item in a sheet - pub fn share_file_path(&self, sheet_name: &SheetName, share_id: &SheetShareId) -> PathBuf { - let sheet_name = snake_case!(sheet_name.clone()); - let share_id = share_id.clone(); - - // Format the path to remove "./" prefix and normalize it - let path_str = SERVER_FILE_SHEET_SHARE - .replace(KEY_SHEET_NAME, &sheet_name) - .replace(KEY_SHARE_ID, &share_id); - - // Use format_path to normalize the path - match format_path::format_path_str(&path_str) { - Ok(normalized_path) => self.vault_path().join(normalized_path), - Err(_) => { - // Fallback to original behavior if formatting fails - self.vault_path().join(path_str) - } - } - } - - /// Get the actual paths of all share items in a sheet - pub async fn share_file_paths(&self, sheet_name: &SheetName) -> Vec { - let sheet_name = snake_case!(sheet_name.clone()); - let shares_dir = self - .vault_path() - .join(SERVER_PATH_SHARES.replace(KEY_SHEET_NAME, &sheet_name)); - - let mut result = Vec::new(); - if let Ok(mut entries) = fs::read_dir(shares_dir).await { - while let Ok(Some(entry)) = entries.next_entry().await { - let path = entry.path(); - if path.is_file() - && path.extension().and_then(|s| s.to_str()) - == Some(SERVER_SUFFIX_SHEET_SHARE_FILE_NO_DOT) - { - result.push(path); - } - } - } - result - } -} - -impl<'a> Sheet<'a> { - /// Get the shares of a sheet - pub async fn get_shares(&self) -> Result, std::io::Error> { - let paths = self.vault_reference.share_file_paths(&self.name).await; - let mut shares = Vec::new(); - - for path in paths { - match Share::read_from(&path).await { - Ok(mut share) => { - share.path = Some(path); - shares.push(share); - } - Err(e) => return Err(e), - } - } - - Ok(shares) - } - - /// Get a share of a sheet - pub async fn get_share(&self, share_id: &SheetShareId) -> Result { - let path = self.vault_reference.share_file_path(&self.name, share_id); - let mut share = Share::read_from(&path).await?; - share.path = Some(path); - Ok(share) - } - - /// Import a share of a sheet by its ID - pub async fn merge_share_by_id( - self, - share_id: &SheetShareId, - share_merge_mode: ShareMergeMode, - ) -> Result<(), std::io::Error> { - let share = self.get_share(share_id).await?; - self.merge_share(share, share_merge_mode).await - } - - /// Import a share of a sheet - pub async fn merge_share( - mut self, - share: Share, - share_merge_mode: ShareMergeMode, - ) -> Result<(), std::io::Error> { - // Backup original data and edit based on this backup - let mut copy_share = share.clone(); - let mut copy_sheet = self.clone_data(); - - // Pre-check - let conflicts = self.precheck(©_share); - let mut reject_mode = false; - - match share_merge_mode { - // Safe mode: conflicts are not allowed - ShareMergeMode::Safe => { - // Conflicts found - if !conflicts.ok() { - // Do nothing, return Error - return Err(Error::new( - std::io::ErrorKind::AlreadyExists, - "Mappings or files already exist!", - )); - } - } - // Overwrite mode: when conflicts occur, prioritize the share item - ShareMergeMode::Overwrite => { - // Handle duplicate mappings - for path in conflicts.duplicate_mapping { - // Get the share data - let Some(share_value) = copy_share.mappings.remove(&path) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - format!("Share value `{}` not found!", &path.display()), - )); - }; - // Overwrite - copy_sheet.mapping_mut().insert(path, share_value); - } - - // Handle duplicate IDs - for path in conflicts.duplicate_file { - // Get the share data - let Some(share_value) = copy_share.mappings.remove(&path) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - format!("Share value `{}` not found!", &path.display()), - )); - }; - - // Extract the file ID - let conflict_vfid = &share_value.id; - - // Through the sheet's ID mapping - let Some(id_mapping) = copy_sheet.id_mapping_mut() else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "Id mapping not found!", - )); - }; - - // Get the original path from the ID mapping - let Some(raw_path) = id_mapping.remove(conflict_vfid) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - format!("The path of virtual file `{}' not found!", conflict_vfid), - )); - }; - - // Remove the original path mapping - if copy_sheet.mapping_mut().remove(&raw_path).is_none() { - return Err(Error::new( - std::io::ErrorKind::NotFound, - format!("Remove mapping `{}` failed!", &raw_path.display()), - )); - } - // Insert the new item - copy_sheet.mapping_mut().insert(path, share_value); - } - } - // Skip mode: when conflicts occur, prioritize the local sheet - ShareMergeMode::Skip => { - // Directly remove conflicting items - for path in conflicts.duplicate_mapping { - copy_share.mappings.remove(&path); - } - for path in conflicts.duplicate_file { - copy_share.mappings.remove(&path); - } - } - // Reject all mode: reject all shares - ShareMergeMode::RejectAll => { - reject_mode = true; // Only mark as rejected - } - } - - if !reject_mode { - // Subsequent merging - copy_sheet - .mapping_mut() - .extend(copy_share.mappings.into_iter()); - - // Merge completed - self.data = copy_sheet; // Write the result - - // Merge completed, consume the sheet - self.persist().await.map_err(|err| { - Error::new( - std::io::ErrorKind::NotFound, - format!("Write sheet failed: {}", err), - ) - })?; - } - - // Persistence succeeded, continue to consume the share item - share.remove().await.map_err(|err| { - Error::new( - std::io::ErrorKind::NotFound, - format!("Remove share failed: {}", err.1), - ) - }) - } - - // Pre-check whether the share can be imported into the current sheet without conflicts - fn precheck(&self, share: &Share) -> ShareMergeConflict { - let mut conflicts = ShareMergeConflict::default(); - - for (mapping, metadata) in &share.mappings { - // Check for duplicate mappings - if self.mapping().contains_key(mapping.as_path()) { - conflicts.duplicate_mapping.push(mapping.clone()); - continue; - } - - // Check for duplicate IDs - if let Some(id_mapping) = self.id_mapping() { - if id_mapping.contains_key(&metadata.id) { - conflicts.duplicate_file.push(mapping.clone()); - continue; - } - } - } - - conflicts - } - - /// Share mappings with another sheet - pub async fn share_mappings( - &self, - other_sheet: &SheetName, - mappings: Vec, - sharer: &MemberId, - description: String, - ) -> Result { - let other_sheet = snake_case!(other_sheet.clone()); - let sharer = snake_case!(sharer.clone()); - - // Check if the sheet exists - let sheet_names = self.vault_reference.sheet_names()?; - if !sheet_names.contains(&other_sheet) { - return Err(Error::new( - std::io::ErrorKind::NotFound, - format!("Sheet `{}` not found!", &other_sheet), - )); - } - - // Check if the target file exists, regenerate ID if path already exists, up to 20 attempts - let target_path = { - let mut id; - let mut share_path; - let mut attempts = 0; - - loop { - id = Share::gen_share_id(&sharer); - share_path = self.vault_reference.share_file_path(&other_sheet, &id); - - if !share_path.exists() { - break share_path; - } - - attempts += 1; - if attempts >= 20 { - return Err(Error::new( - std::io::ErrorKind::AlreadyExists, - "Failed to generate unique share ID after 20 attempts!", - )); - } - } - }; - - // Validate that the share is valid - let mut share_mappings = HashMap::new(); - for mapping_path in &mappings { - if let Some(metadata) = self.mapping().get(mapping_path) { - share_mappings.insert(mapping_path.clone(), metadata.clone()); - } else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - format!("Mapping `{}` not found in sheet!", mapping_path.display()), - )); - } - } - - // Build share data - let share_data = Share { - sharer, - description, - path: None, // This is only needed during merging (reading), no need to serialize now - from_sheet: self.name.clone(), - mappings: share_mappings, - }; - - // Write data - Share::write_to(&share_data, target_path).await?; - - Ok(share_data) - } -} - -impl Share { - /// Generate a share ID for a given sharer - pub fn gen_share_id(sharer: &MemberId) -> String { - let sharer_snake = snake_case!(sharer.clone()); - let random_part: String = rng() - .sample_iter(&rand::distr::Alphanumeric) - .take(8) - .map(char::from) - .collect(); - format!("{}@{}", sharer_snake, random_part) - } - - /// Delete a share (reject or remove the share item) - /// If deletion succeeds, returns `Ok(())`; - /// If deletion fails, returns `Err((self, std::io::Error))`, containing the original share object and the error information. - pub async fn remove(self) -> Result<(), (Self, std::io::Error)> { - let Some(path) = &self.path else { - return Err(( - self, - Error::new(std::io::ErrorKind::NotFound, "No share path recorded!"), - )); - }; - - if !path.exists() { - return Err(( - self, - Error::new(std::io::ErrorKind::NotFound, "No share file exists!"), - )); - } - - match fs::remove_file(path).await { - Err(err) => Err(( - self, - Error::new( - std::io::ErrorKind::Other, - format!("Failed to delete share file: {}", err), - ), - )), - Ok(_) => Ok(()), - } - } -} diff --git a/data/src/data/vault/member_manage.rs b/data/src/data/vault/member_manage.rs deleted file mode 100644 index 9d22d09..0000000 --- a/data/src/data/vault/member_manage.rs +++ /dev/null @@ -1,144 +0,0 @@ -use std::{ - fs, - io::{Error, ErrorKind}, - path::PathBuf, -}; - -use cfg_file::config::ConfigFile; - -use crate::{ - constants::{ - SERVER_FILE_MEMBER_INFO, SERVER_FILE_MEMBER_PUB, SERVER_PATH_MEMBERS, - SERVER_SUFFIX_MEMBER_INFO_NO_DOT, - }, - data::{ - member::{Member, MemberId}, - vault::Vault, - }, -}; - -const ID_PARAM: &str = "{member_id}"; - -/// Member Manage -impl Vault { - /// Read member from configuration file - pub async fn member(&self, id: &MemberId) -> Result { - if let Some(cfg_file) = self.member_cfg(id) { - let member = Member::read_from(cfg_file).await?; - return Ok(member); - } - - Err(Error::new(ErrorKind::NotFound, "Member not found!")) - } - - /// List all member IDs in the vault - pub fn member_ids(&self) -> Result, std::io::Error> { - let members_path = self.vault_path.join(SERVER_PATH_MEMBERS); - - if !members_path.exists() { - return Ok(Vec::new()); - } - - let mut member_ids = Vec::new(); - - for entry in fs::read_dir(members_path)? { - let entry = entry?; - let path = entry.path(); - - if path.is_file() - && let Some(file_name) = path.file_stem().and_then(|s| s.to_str()) - && path.extension().and_then(|s| s.to_str()) - == Some(SERVER_SUFFIX_MEMBER_INFO_NO_DOT) - { - member_ids.push(file_name.to_string()); - } - } - - Ok(member_ids) - } - - /// Get all members - /// This method will read and deserialize member information, please pay attention to performance issues - pub async fn members(&self) -> Result, std::io::Error> { - let mut members = Vec::new(); - - for member_id in self.member_ids()? { - if let Ok(member) = self.member(&member_id).await { - members.push(member); - } - } - - Ok(members) - } - - /// Update member info - pub async fn update_member(&self, member: Member) -> Result<(), std::io::Error> { - // Ensure member exist - if self.member_cfg(&member.id()).is_some() { - let member_cfg_path = self.member_cfg_path(&member.id()); - Member::write_to(&member, member_cfg_path).await?; - return Ok(()); - } - - Err(Error::new(ErrorKind::NotFound, "Member not found!")) - } - - /// Register a member to vault - pub async fn register_member_to_vault(&self, member: Member) -> Result<(), std::io::Error> { - // Ensure member not exist - if self.member_cfg(&member.id()).is_some() { - return Err(Error::new( - ErrorKind::DirectoryNotEmpty, - format!("Member `{}` already registered!", member.id()), - )); - } - - // Wrtie config file to member dir - let member_cfg_path = self.member_cfg_path(&member.id()); - Member::write_to(&member, member_cfg_path).await?; - - Ok(()) - } - - /// Remove member from vault - pub fn remove_member_from_vault(&self, id: &MemberId) -> Result<(), std::io::Error> { - // Ensure member exist - if let Some(member_cfg_path) = self.member_cfg(id) { - fs::remove_file(member_cfg_path)?; - } - - Ok(()) - } - - /// Try to get the member's configuration file to determine if the member exists - pub fn member_cfg(&self, id: &MemberId) -> Option { - let cfg_file = self.member_cfg_path(id); - if cfg_file.exists() { - Some(cfg_file) - } else { - None - } - } - - /// Try to get the member's public key file to determine if the member has login permission - pub fn member_key(&self, id: &MemberId) -> Option { - let key_file = self.member_key_path(id); - if key_file.exists() { - Some(key_file) - } else { - None - } - } - - /// Get the member's configuration file path, but do not check if the file exists - pub fn member_cfg_path(&self, id: &MemberId) -> PathBuf { - self.vault_path - .join(SERVER_FILE_MEMBER_INFO.replace(ID_PARAM, id.to_string().as_str())) - } - - /// Get the member's public key file path, but do not check if the file exists - pub fn member_key_path(&self, id: &MemberId) -> PathBuf { - self.vault_path - .join(SERVER_FILE_MEMBER_PUB.replace(ID_PARAM, id.to_string().as_str())) - } -} diff --git a/data/src/data/vault/sheet_manage.rs b/data/src/data/vault/sheet_manage.rs deleted file mode 100644 index c22c849..0000000 --- a/data/src/data/vault/sheet_manage.rs +++ /dev/null @@ -1,274 +0,0 @@ -use std::{collections::HashMap, io::Error}; - -use cfg_file::config::ConfigFile; -use string_proc::snake_case; -use tokio::fs; - -use crate::{ - constants::{SERVER_PATH_SHEETS, SERVER_SUFFIX_SHEET_FILE_NO_DOT}, - data::{ - member::MemberId, - sheet::{Sheet, SheetData, SheetName}, - vault::Vault, - }, -}; - -/// Vault Sheets Management -impl Vault { - /// Load all sheets in the vault - /// - /// It is generally not recommended to call this function frequently. - /// Although a vault typically won't contain too many sheets, - /// if individual sheet contents are large, this operation may cause - /// significant performance bottlenecks. - pub async fn sheets<'a>(&'a self) -> Result>, std::io::Error> { - let sheet_names = self.sheet_names()?; - let mut sheets = Vec::new(); - - for sheet_name in sheet_names { - let sheet = self.sheet(&sheet_name).await?; - sheets.push(sheet); - } - - Ok(sheets) - } - - /// Search for all sheet names in the vault - /// - /// The complexity of this operation is proportional to the number of sheets, - /// but generally there won't be too many sheets in a Vault - pub fn sheet_names(&self) -> Result, std::io::Error> { - // Get the sheets directory path - let sheets_dir = self.vault_path.join(SERVER_PATH_SHEETS); - - // If the directory doesn't exist, return an empty list - if !sheets_dir.exists() { - return Ok(vec![]); - } - - let mut sheet_names = Vec::new(); - - // Iterate through all files in the sheets directory - for entry in std::fs::read_dir(sheets_dir)? { - let entry = entry?; - let path = entry.path(); - - // Check if it's a YAML file - if path.is_file() - && path - .extension() - .is_some_and(|ext| ext == SERVER_SUFFIX_SHEET_FILE_NO_DOT) - && let Some(file_stem) = path.file_stem().and_then(|s| s.to_str()) - { - // Create a new SheetName and add it to the result list - sheet_names.push(file_stem.to_string()); - } - } - - Ok(sheet_names) - } - - /// Read a sheet from its name - /// - /// If the sheet information is successfully found in the vault, - /// it will be deserialized and read as a sheet. - /// This is the only correct way to obtain a sheet instance. - pub async fn sheet<'a>(&'a self, sheet_name: &SheetName) -> Result, std::io::Error> { - let sheet_name = snake_case!(sheet_name.clone()); - - // Get the path to the sheet file - let sheet_path = Sheet::sheet_path_with_name(self, &sheet_name); - - // Ensure the sheet file exists - if !sheet_path.exists() { - // If the sheet does not exist, try to restore it from the trash - if self.restore_sheet(&sheet_name).await.is_err() { - // If restoration fails, return an error - return Err(Error::new( - std::io::ErrorKind::NotFound, - format!("Sheet `{}` not found!", sheet_name), - )); - } - } - - // Read the sheet data from the file - let data = SheetData::read_from(sheet_path).await?; - - Ok(Sheet { - name: sheet_name.clone(), - data, - vault_reference: self, - }) - } - - /// Create a sheet locally and return the sheet instance - /// - /// This method creates a new sheet in the vault with the given name and holder. - /// It will verify that the member exists and that the sheet doesn't already exist - /// before creating the sheet file with default empty data. - pub async fn create_sheet<'a>( - &'a self, - sheet_name: &SheetName, - holder: &MemberId, - ) -> Result, std::io::Error> { - let sheet_name = snake_case!(sheet_name.clone()); - - // Ensure member exists - if !self.member_cfg_path(holder).exists() { - return Err(Error::new( - std::io::ErrorKind::NotFound, - format!("Member `{}` not found!", &holder), - )); - } - - // Ensure sheet does not already exist - let sheet_file_path = Sheet::sheet_path_with_name(self, &sheet_name); - if sheet_file_path.exists() { - return Err(Error::new( - std::io::ErrorKind::AlreadyExists, - format!("Sheet `{}` already exists!", &sheet_name), - )); - } - - // Create the sheet file - let sheet_data = SheetData { - holder: Some(holder.clone()), - mapping: HashMap::new(), - id_mapping: None, - write_count: 0, - }; - SheetData::write_to(&sheet_data, sheet_file_path).await?; - - Ok(Sheet { - name: sheet_name, - data: sheet_data, - vault_reference: self, - }) - } - - /// Delete the sheet file from local disk by name - /// - /// This method will remove the sheet file with the given name from the vault. - /// It will verify that the sheet exists before attempting to delete it. - /// If the sheet is successfully deleted, it will return Ok(()). - /// - /// Warning: This operation is dangerous. Deleting a sheet will cause local workspaces - /// using this sheet to become invalid. Please ensure the sheet is not currently in use - /// and will not be used in the future. - /// - /// For a safer deletion method, consider using `delete_sheet_safety`. - /// - /// Note: This function is intended for server-side use only and should not be - /// arbitrarily called by other members to prevent unauthorized data deletion. - pub async fn delete_sheet(&self, sheet_name: &SheetName) -> Result<(), std::io::Error> { - let sheet_name = snake_case!(sheet_name.clone()); - - // Ensure sheet exists - let sheet_file_path = Sheet::sheet_path_with_name(self, &sheet_name); - if !sheet_file_path.exists() { - return Err(Error::new( - std::io::ErrorKind::NotFound, - format!("Sheet `{}` not found!", &sheet_name), - )); - } - - // Delete the sheet file - fs::remove_file(sheet_file_path).await?; - - Ok(()) - } - - /// Safely delete the sheet - /// - /// The sheet will be moved to the trash directory, ensuring it does not appear in the - /// results of `sheets` and `sheet_names` methods. - /// However, if the sheet's holder attempts to access the sheet through the `sheet` method, - /// the system will automatically restore it from the trash directory. - /// This means: the sheet will only permanently remain in the trash directory, - /// waiting for manual cleanup by an administrator, when it is truly no longer in use. - /// - /// This is a safer deletion method because it provides the possibility of recovery, - /// avoiding irreversible data loss caused by accidental deletion. - /// - /// Note: This function is intended for server-side use only and should not be - /// arbitrarily called by other members to prevent unauthorized data deletion. - pub async fn delete_sheet_safely(&self, sheet_name: &SheetName) -> Result<(), std::io::Error> { - let sheet_name = snake_case!(sheet_name.clone()); - - // Ensure the sheet exists - let sheet_file_path = Sheet::sheet_path_with_name(self, &sheet_name); - if !sheet_file_path.exists() { - return Err(Error::new( - std::io::ErrorKind::NotFound, - format!("Sheet `{}` not found!", &sheet_name), - )); - } - - // Create the trash directory - let trash_dir = self.vault_path.join(".trash"); - if !trash_dir.exists() { - fs::create_dir_all(&trash_dir).await?; - } - - // Generate a unique filename in the trash - let timestamp = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_millis(); - let trash_file_name = format!( - "{}_{}.{}", - sheet_name, timestamp, SERVER_SUFFIX_SHEET_FILE_NO_DOT - ); - let trash_path = trash_dir.join(trash_file_name); - - // Move the sheet file to the trash - fs::rename(&sheet_file_path, &trash_path).await?; - - Ok(()) - } - - /// Restore the sheet from the trash - /// - /// Restore the specified sheet from the trash to its original location, making it accessible normally. - pub async fn restore_sheet(&self, sheet_name: &SheetName) -> Result<(), std::io::Error> { - let sheet_name = snake_case!(sheet_name.clone()); - - // Search for matching files in the trash - let trash_dir = self.vault_path.join(".trash"); - if !trash_dir.exists() { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "Trash directory does not exist!".to_string(), - )); - } - - let mut found_path = None; - for entry in std::fs::read_dir(&trash_dir)? { - let entry = entry?; - let path = entry.path(); - - if path.is_file() - && let Some(file_name) = path.file_stem().and_then(|s| s.to_str()) - { - // Check if the filename starts with the sheet name - if file_name.starts_with(&sheet_name) { - found_path = Some(path); - break; - } - } - } - - let trash_path = found_path.ok_or_else(|| { - Error::new( - std::io::ErrorKind::NotFound, - format!("Sheet `{}` not found in trash!", &sheet_name), - ) - })?; - - // Restore the sheet to its original location - let original_path = Sheet::sheet_path_with_name(self, &sheet_name); - fs::rename(&trash_path, &original_path).await?; - - Ok(()) - } -} diff --git a/data/src/data/vault/vault_config.rs b/data/src/data/vault/vault_config.rs deleted file mode 100644 index caa8552..0000000 --- a/data/src/data/vault/vault_config.rs +++ /dev/null @@ -1,233 +0,0 @@ -use std::net::{IpAddr, Ipv4Addr}; - -use cfg_file::ConfigFile; -use serde::{Deserialize, Serialize}; -use uuid::Uuid; - -use crate::constants::{PORT, SERVER_FILE_VAULT}; -use crate::data::member::{Member, MemberId}; - -pub type VaultName = String; -pub type VaultUuid = Uuid; - -#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] -#[serde(rename_all = "lowercase")] -pub enum AuthMode { - /// Use asymmetric keys: both client and server need to register keys, after which they can connect - Key, - - /// Use password: the password stays on the server, and the client needs to set the password locally for connection - #[default] - Password, - - /// No authentication: generally used in a strongly secure environment, skipping verification directly - NoAuth, -} - -#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] -#[serde(rename_all = "lowercase")] -pub enum LoggerLevel { - Debug, - Trace, - - #[default] - Info, -} - -#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] -#[serde(rename_all = "lowercase")] -pub enum ServiceEnabled { - Enable, - - #[default] - Disable, -} - -#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] -#[serde(rename_all = "lowercase")] -pub enum BehaviourEnabled { - Yes, - - #[default] - No, -} - -impl Into for ServiceEnabled { - fn into(self) -> bool { - match self { - ServiceEnabled::Enable => true, - ServiceEnabled::Disable => false, - } - } -} - -impl Into for BehaviourEnabled { - fn into(self) -> bool { - match self { - BehaviourEnabled::Yes => true, - BehaviourEnabled::No => false, - } - } -} - -#[derive(Serialize, Deserialize, ConfigFile)] -#[cfg_file(path = SERVER_FILE_VAULT)] -pub struct VaultConfig { - /// Vault uuid, unique identifier for the vault - #[serde(rename = "uuid")] - vault_uuid: VaultUuid, - - /// Vault name, which can be used as the project name and generally serves as a hint - #[serde(rename = "name")] - vault_name: VaultName, - - /// Vault host ids, a list of member id representing administrator identities - #[serde(rename = "hosts")] - vault_host_list: Vec, - - /// Vault server configuration, which will be loaded when connecting to the server - #[serde(rename = "profile")] - server_config: VaultServerConfig, -} - -#[derive(Serialize, Deserialize)] -pub struct VaultServerConfig { - /// Local IP address to bind to when the server starts - #[serde(rename = "bind")] - local_bind: IpAddr, - - /// TCP port to bind to when the server starts - #[serde(rename = "port")] - port: u16, - - /// Enable logging - #[serde(rename = "logger")] - logger: Option, - - /// Logger Level - #[serde(rename = "logger_level")] - logger_level: Option, - - /// Whether to enable LAN discovery, allowing members on the same LAN to more easily find the upstream server - #[serde(rename = "lan_discovery")] - lan_discovery: Option, // TODO - - /// Authentication mode for the vault server - /// key: Use asymmetric keys for authentication - /// password: Use a password for authentication - /// noauth: No authentication required, requires a strongly secure environment - #[serde(rename = "auth_mode")] - auth_mode: Option, // TODO -} - -impl Default for VaultConfig { - fn default() -> Self { - Self { - vault_uuid: Uuid::new_v4(), - vault_name: "JustEnoughVault".to_string(), - vault_host_list: Vec::new(), - server_config: VaultServerConfig { - local_bind: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), - port: PORT, - logger: Some(BehaviourEnabled::default()), - logger_level: Some(LoggerLevel::default()), - lan_discovery: Some(ServiceEnabled::default()), - auth_mode: Some(AuthMode::Key), - }, - } - } -} - -/// Vault Management -impl VaultConfig { - /// Change name of the vault. - pub fn change_name(&mut self, name: impl Into) { - self.vault_name = name.into() - } - - /// Add admin - pub fn add_admin(&mut self, member: &Member) { - let uuid = member.id(); - if !self.vault_host_list.contains(&uuid) { - self.vault_host_list.push(uuid); - } - } - - /// Remove admin - pub fn remove_admin(&mut self, member: &Member) { - let id = member.id(); - self.vault_host_list.retain(|x| x != &id); - } - - /// Get vault UUID - pub fn vault_uuid(&self) -> &VaultUuid { - &self.vault_uuid - } - - /// Set vault UUID - pub fn set_vault_uuid(&mut self, vault_uuid: VaultUuid) { - self.vault_uuid = vault_uuid; - } - - /// Get vault name - pub fn vault_name(&self) -> &VaultName { - &self.vault_name - } - - /// Set vault name - pub fn set_vault_name(&mut self, vault_name: VaultName) { - self.vault_name = vault_name; - } - - /// Get vault admin list - pub fn vault_host_list(&self) -> &Vec { - &self.vault_host_list - } - - /// Set vault admin list - pub fn set_vault_host_list(&mut self, vault_host_list: Vec) { - self.vault_host_list = vault_host_list; - } - - /// Get server config - pub fn server_config(&self) -> &VaultServerConfig { - &self.server_config - } - - /// Set server config - pub fn set_server_config(&mut self, server_config: VaultServerConfig) { - self.server_config = server_config; - } -} - -impl VaultServerConfig { - /// Get local bind IP address - pub fn local_bind(&self) -> &IpAddr { - &self.local_bind - } - - /// Get port - pub fn port(&self) -> u16 { - self.port - } - - /// Check if LAN discovery is enabled - pub fn is_lan_discovery_enabled(&self) -> bool { - self.lan_discovery.clone().unwrap_or_default().into() - } - - /// Get logger enabled status - pub fn is_logger_enabled(&self) -> bool { - self.logger.clone().unwrap_or_default().into() - } - - /// Get logger level - pub fn logger_level(&self) -> LoggerLevel { - self.logger_level.clone().unwrap_or_default() - } - - /// Get authentication mode - pub fn auth_mode(&self) -> AuthMode { - self.auth_mode.clone().unwrap_or_default() - } -} diff --git a/data/src/data/vault/virtual_file.rs b/data/src/data/vault/virtual_file.rs deleted file mode 100644 index 28e9172..0000000 --- a/data/src/data/vault/virtual_file.rs +++ /dev/null @@ -1,500 +0,0 @@ -use std::{ - collections::HashMap, - io::{Error, ErrorKind}, - path::PathBuf, -}; - -use cfg_file::{ConfigFile, config::ConfigFile}; -use serde::{Deserialize, Serialize}; -use string_proc::{dot_case, snake_case}; -use tcp_connection::instance::ConnectionInstance; -use tokio::fs; -use uuid::Uuid; - -use crate::{ - constants::{ - DEFAULT_VF_DESCRIPTION, DEFAULT_VF_VERSION, KEY_TEMP_NAME, KEY_VF_ID, KEY_VF_INDEX, - KEY_VF_VERSION, SERVER_FILE_VF_META, SERVER_FILE_VF_VERSION_INSTANCE, SERVER_PATH_VF_ROOT, - SERVER_PATH_VF_STORAGE, SERVER_PATH_VF_TEMP, VF_PREFIX, - }, - data::{member::MemberId, vault::Vault}, -}; - -pub type VirtualFileId = String; -pub type VirtualFileVersion = String; - -pub struct VirtualFile<'a> { - /// Unique identifier for the virtual file - id: VirtualFileId, - - /// Reference of Vault - current_vault: &'a Vault, -} - -#[derive(Default, Clone, Serialize, Deserialize, ConfigFile)] -pub struct VirtualFileMeta { - /// Current version of the virtual file - #[serde(rename = "ver")] - current_version: VirtualFileVersion, - - /// The member who holds the edit right of the file - #[serde(rename = "holder")] - hold_member: MemberId, - - /// Description of each version - #[serde(rename = "descs")] - version_description: HashMap, - - /// Histories - #[serde(rename = "histories")] - histories: Vec, -} - -#[derive(Debug, Default, Clone, Serialize, Deserialize)] -pub struct VirtualFileVersionDescription { - /// The member who created this version - #[serde(rename = "creator")] - pub creator: MemberId, - - /// The description of this version - #[serde(rename = "desc")] - pub description: String, -} - -impl VirtualFileVersionDescription { - /// Create a new version description - pub fn new(creator: MemberId, description: String) -> Self { - Self { - creator, - description, - } - } -} - -/// Virtual File Operations -impl Vault { - /// Generate a temporary path for receiving - pub fn virtual_file_temp_path(&self) -> PathBuf { - let random_receive_name = format!("{}", uuid::Uuid::new_v4()); - self.vault_path - .join(SERVER_PATH_VF_TEMP.replace(KEY_TEMP_NAME, &random_receive_name)) - } - - /// Get the directory where virtual files are stored - pub fn virtual_file_storage_dir(&self) -> PathBuf { - self.vault_path().join(SERVER_PATH_VF_ROOT) - } - - /// Get the directory where a specific virtual file is stored - pub fn virtual_file_dir(&self, id: &VirtualFileId) -> Result { - Ok(self.vault_path().join( - SERVER_PATH_VF_STORAGE - .replace(KEY_VF_ID, &id.to_string()) - .replace(KEY_VF_INDEX, &Self::vf_index(id)?), - )) - } - - // Generate index path of virtual file - fn vf_index(id: &VirtualFileId) -> Result { - // Remove VF_PREFIX if present - let id_str = if let Some(stripped) = id.strip_prefix(VF_PREFIX) { - stripped - } else { - id - }; - - // Extract the first part before the first hyphen - let first_part = id_str.split('-').next().ok_or_else(|| { - std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "Invalid virtual file ID format: no hyphen found", - ) - })?; - - // Ensure the first part has at least 4 characters - if first_part.len() < 4 { - return Err(std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "Invalid virtual file ID format: first part must have at least 4 characters", - ))?; - } - - // Take only the first 4 characters and split into two 2-character chunks - let first_four = &first_part[0..4]; - let mut path = String::new(); - for i in (0..first_four.len()).step_by(2) { - if i > 0 { - path.push('/'); - } - path.push_str(&first_four[i..i + 2]); - } - - Ok(path) - } - - /// Get the directory where a specific virtual file's metadata is stored - pub fn virtual_file_real_path( - &self, - id: &VirtualFileId, - version: &VirtualFileVersion, - ) -> PathBuf { - self.vault_path().join( - SERVER_FILE_VF_VERSION_INSTANCE - .replace(KEY_VF_ID, &id.to_string()) - .replace(KEY_VF_INDEX, &Self::vf_index(id).unwrap_or_default()) - .replace(KEY_VF_VERSION, &version.to_string()), - ) - } - - /// Get the directory where a specific virtual file's metadata is stored - pub fn virtual_file_meta_path(&self, id: &VirtualFileId) -> PathBuf { - self.vault_path().join( - SERVER_FILE_VF_META - .replace(KEY_VF_ID, &id.to_string()) - .replace(KEY_VF_INDEX, &Self::vf_index(id).unwrap_or_default()), - ) - } - - /// Get the virtual file with the given ID - pub fn virtual_file(&self, id: &VirtualFileId) -> Result, std::io::Error> { - let dir = self.virtual_file_dir(id); - if dir?.exists() { - Ok(VirtualFile { - id: id.clone(), - current_vault: self, - }) - } else { - Err(std::io::Error::new( - std::io::ErrorKind::NotFound, - "Cannot found virtual file!", - )) - } - } - - /// Get the meta data of the virtual file with the given ID - pub async fn virtual_file_meta( - &self, - id: &VirtualFileId, - ) -> Result { - let dir = self.virtual_file_meta_path(id); - let metadata = VirtualFileMeta::read_from(dir).await?; - Ok(metadata) - } - - /// Write the meta data of the virtual file with the given ID - pub async fn write_virtual_file_meta( - &self, - id: &VirtualFileId, - meta: &VirtualFileMeta, - ) -> Result<(), std::io::Error> { - let dir = self.virtual_file_meta_path(id); - VirtualFileMeta::write_to(meta, dir).await?; - Ok(()) - } - - /// Create a virtual file from a connection instance - /// - /// It's the only way to create virtual files! - /// - /// When the target machine executes `write_file`, use this function instead of `read_file`, - /// and provide the member ID of the transmitting member. - /// - /// The system will automatically receive the file and - /// create the virtual file. - pub async fn create_virtual_file_from_connection( - &self, - instance: &mut ConnectionInstance, - member_id: &MemberId, - ) -> Result { - let receive_path = self.virtual_file_temp_path(); - let new_id = format!("{}{}", VF_PREFIX, Uuid::new_v4()); - let move_path = self.virtual_file_real_path(&new_id, &DEFAULT_VF_VERSION.to_string()); - - match instance.read_file(receive_path.clone()).await { - Ok(_) => { - // Read successful, create virtual file - // Create default version description - let mut version_description = - HashMap::::new(); - version_description.insert( - DEFAULT_VF_VERSION.to_string(), - VirtualFileVersionDescription { - creator: member_id.clone(), - description: DEFAULT_VF_DESCRIPTION.to_string(), - }, - ); - // Create metadata - let mut meta = VirtualFileMeta { - current_version: DEFAULT_VF_VERSION.to_string(), - hold_member: member_id.clone(), // The holder of the newly created virtual file is the creator by default - version_description, - histories: Vec::default(), - }; - - // Add first version - meta.histories.push(DEFAULT_VF_VERSION.to_string()); - - // Write metadata to file - VirtualFileMeta::write_to(&meta, self.virtual_file_meta_path(&new_id)).await?; - - // Move temp file to virtual file directory - if let Some(parent) = move_path.parent() - && !parent.exists() - { - fs::create_dir_all(parent).await?; - } - fs::rename(receive_path, move_path).await?; - - Ok(new_id) - } - Err(e) => { - // Read failed, remove temp file. - if receive_path.exists() { - fs::remove_file(receive_path).await?; - } - - Err(Error::other(e)) - } - } - } - - /// Update a virtual file from a connection instance - /// - /// It's the only way to update virtual files! - /// When the target machine executes `write_file`, use this function instead of `read_file`, - /// and provide the member ID of the transmitting member. - /// - /// The system will automatically receive the file and - /// update the virtual file. - /// - /// Note: The specified member must hold the edit right of the file, - /// otherwise the file reception will not be allowed. - /// - /// Make sure to obtain the edit right of the file before calling this function. - pub async fn update_virtual_file_from_connection( - &self, - instance: &mut ConnectionInstance, - member: &MemberId, - virtual_file_id: &VirtualFileId, - new_version: &VirtualFileVersion, - description: VirtualFileVersionDescription, - ) -> Result<(), std::io::Error> { - let new_version = dot_case!(new_version.clone()); - let mut meta = self.virtual_file_meta(virtual_file_id).await?; - - // Check if the member has edit right - self.check_virtual_file_edit_right(member, virtual_file_id) - .await?; - - // Check if the new version already exists - if meta.version_description.contains_key(&new_version) { - return Err(Error::new( - ErrorKind::AlreadyExists, - format!( - "Version `{}` already exists for virtual file `{}`", - new_version, virtual_file_id - ), - )); - } - - // Verify success - let receive_path = self.virtual_file_temp_path(); - let move_path = self.virtual_file_real_path(virtual_file_id, &new_version); - - match instance.read_file(receive_path.clone()).await { - Ok(_) => { - // Read success, move temp file to real path. - fs::rename(receive_path, move_path).await?; - - // Update metadata - meta.current_version = new_version.clone(); - meta.version_description - .insert(new_version.clone(), description); - meta.histories.push(new_version); - VirtualFileMeta::write_to(&meta, self.virtual_file_meta_path(virtual_file_id)) - .await?; - - Ok(()) - } - Err(e) => { - // Read failed, remove temp file. - if receive_path.exists() { - fs::remove_file(receive_path).await?; - } - - Err(Error::other(e)) - } - } - } - - /// Update virtual file from existing version - /// - /// This operation creates a new version based on the specified old version file instance. - /// The new version will retain the same version name as the old version, but use a different version number. - /// After the update, this version will be considered newer than the original version when comparing versions. - pub async fn update_virtual_file_from_exist_version( - &self, - member: &MemberId, - virtual_file_id: &VirtualFileId, - old_version: &VirtualFileVersion, - ) -> Result<(), std::io::Error> { - let old_version = snake_case!(old_version.clone()); - let mut meta = self.virtual_file_meta(virtual_file_id).await?; - - // Check if the member has edit right - self.check_virtual_file_edit_right(member, virtual_file_id) - .await?; - - // Ensure virtual file exist - let Ok(_) = self.virtual_file(virtual_file_id) else { - return Err(Error::new( - ErrorKind::NotFound, - format!("Virtual file `{}` not found!", virtual_file_id), - )); - }; - - // Ensure version exist - if !meta.version_exists(&old_version) { - return Err(Error::new( - ErrorKind::NotFound, - format!("Version `{}` not found!", old_version), - )); - } - - // Ok, Create new version - meta.current_version = old_version.clone(); - meta.histories.push(old_version); - VirtualFileMeta::write_to(&meta, self.virtual_file_meta_path(virtual_file_id)).await?; - - Ok(()) - } - - /// Grant a member the edit right for a virtual file - /// This operation takes effect immediately upon success - pub async fn grant_virtual_file_edit_right( - &self, - member_id: &MemberId, - virtual_file_id: &VirtualFileId, - ) -> Result<(), std::io::Error> { - let mut meta = self.virtual_file_meta(virtual_file_id).await?; - meta.hold_member = member_id.clone(); - self.write_virtual_file_meta(virtual_file_id, &meta).await - } - - /// Check if a member has the edit right for a virtual file - pub async fn has_virtual_file_edit_right( - &self, - member_id: &MemberId, - virtual_file_id: &VirtualFileId, - ) -> Result { - let meta = self.virtual_file_meta(virtual_file_id).await?; - Ok(meta.hold_member.eq(member_id)) - } - - /// Check if a member has the edit right for a virtual file and return Result - /// Returns Ok(()) if the member has edit right, otherwise returns PermissionDenied error - pub async fn check_virtual_file_edit_right( - &self, - member_id: &MemberId, - virtual_file_id: &VirtualFileId, - ) -> Result<(), std::io::Error> { - if !self - .has_virtual_file_edit_right(member_id, virtual_file_id) - .await? - { - return Err(Error::new( - ErrorKind::PermissionDenied, - format!( - "Member `{}` not allowed to update virtual file `{}`", - member_id, virtual_file_id - ), - )); - } - Ok(()) - } - - /// Revoke the edit right for a virtual file from the current holder - /// This operation takes effect immediately upon success - pub async fn revoke_virtual_file_edit_right( - &self, - virtual_file_id: &VirtualFileId, - ) -> Result<(), std::io::Error> { - let mut meta = self.virtual_file_meta(virtual_file_id).await?; - meta.hold_member = String::default(); - self.write_virtual_file_meta(virtual_file_id, &meta).await - } -} - -impl<'a> VirtualFile<'a> { - /// Get id of VirtualFile - pub fn id(&self) -> VirtualFileId { - self.id.clone() - } - - /// Read metadata of VirtualFile - pub async fn read_meta(&self) -> Result { - self.current_vault.virtual_file_meta(&self.id).await - } -} - -impl VirtualFileMeta { - /// Get all versions of the virtual file - pub fn versions(&self) -> &Vec { - &self.histories - } - - /// Get the latest version of the virtual file - pub fn version_latest(&self) -> VirtualFileVersion { - // After creating a virtual file in `update_virtual_file_from_connection`, - // the Vec will never be empty, so unwrap is allowed here - self.histories.last().unwrap().clone() - } - - /// Get the total number of versions for this virtual file - pub fn version_len(&self) -> i32 { - self.histories.len() as i32 - } - - /// Check if a specific version exists - /// Returns true if the version exists, false otherwise - pub fn version_exists(&self, version: &VirtualFileVersion) -> bool { - self.versions().iter().any(|v| v == version) - } - - /// Get the version number (index) for a given version name - /// Returns None if the version doesn't exist - pub fn version_num(&self, version: &VirtualFileVersion) -> Option { - self.histories - .iter() - .rev() - .position(|v| v == version) - .map(|pos| (self.histories.len() - 1 - pos) as i32) - } - - /// Get the version name for a given version number (index) - /// Returns None if the version number is out of range - pub fn version_name(&self, version_num: i32) -> Option { - self.histories.get(version_num as usize).cloned() - } - - /// Get the member who holds the edit right of the file - pub fn hold_member(&self) -> &MemberId { - &self.hold_member - } - - /// Get the version descriptions for all versions - pub fn version_descriptions( - &self, - ) -> &HashMap { - &self.version_description - } - - /// Get the version description for a given version - pub fn version_description( - &self, - version: VirtualFileVersion, - ) -> Option<&VirtualFileVersionDescription> { - let desc = self.version_descriptions(); - desc.get(&version) - } -} diff --git a/data/src/env.rs b/data/src/env.rs deleted file mode 100644 index 209c0cc..0000000 --- a/data/src/env.rs +++ /dev/null @@ -1,84 +0,0 @@ -use crate::constants::*; -use std::io::{self, Error}; -use std::{env::set_current_dir, path::PathBuf}; - -/// Find the nearest vault or local workspace and correct the `current_dir` to it -pub fn correct_current_dir() -> Result<(), io::Error> { - if let Some(local_workspace) = current_local_path() { - set_current_dir(local_workspace)?; - return Ok(()); - } - if let Some(vault) = current_vault_path() { - set_current_dir(vault)?; - return Ok(()); - } - Err(Error::new( - io::ErrorKind::NotFound, - "Could not find any vault or local workspace!", - )) -} - -/// Get the nearest Vault directory from `current_dir` -pub fn current_vault_path() -> Option { - let current_dir = std::env::current_dir().ok()?; - find_vault_path(current_dir) -} - -/// Get the nearest local workspace from `current_dir` -pub fn current_local_path() -> Option { - let current_dir = std::env::current_dir().ok()?; - find_local_path(current_dir) -} - -/// Get the nearest Vault directory from the specified path -pub fn find_vault_path(path: impl Into) -> Option { - let mut current_path = path.into(); - let vault_file = SERVER_FILE_VAULT; - - loop { - let vault_toml_path = current_path.join(vault_file); - if vault_toml_path.exists() { - return Some(current_path); - } - - if let Some(parent) = current_path.parent() { - current_path = parent.to_path_buf(); - } else { - break; - } - } - - None -} - -/// Get the nearest local workspace from the specified path -pub fn find_local_path(path: impl Into) -> Option { - let mut current_path = path.into(); - let workspace_dir = CLIENT_PATH_WORKSPACE_ROOT; - - loop { - let jvc_path = current_path.join(workspace_dir); - if jvc_path.exists() { - return Some(current_path); - } - - if let Some(parent) = current_path.parent() { - current_path = parent.to_path_buf(); - } else { - break; - } - } - - None -} - -/// Get the system's document directory and join with the appropriate application name -pub fn current_cfg_dir() -> Option { - dirs::config_local_dir().map(|path| { - if cfg!(target_os = "linux") { - path.join("jvcs") - } else { - path.join("JustEnoughVCS") - } - }) -} diff --git a/data/src/lib.rs b/data/src/lib.rs deleted file mode 100644 index df2ec34..0000000 --- a/data/src/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub mod constants; -pub mod env; - -#[allow(dead_code)] -pub mod data; diff --git a/data/tests/Cargo.toml b/data/tests/Cargo.toml deleted file mode 100644 index e7a0fcc..0000000 --- a/data/tests/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "vcs_data_test" -edition = "2024" -version.workspace = true - -[dependencies] -tcp_connection = { path = "../../utils/tcp_connection" } -tcp_connection_test = { path = "../../utils/tcp_connection/tcp_connection_test" } -cfg_file = { path = "../../utils/cfg_file", features = ["default"] } -vcs_data = { path = "../../data" } - -# Async & Networking -tokio = { version = "1.48.0", features = ["full"] } diff --git a/data/tests/src/lib.rs b/data/tests/src/lib.rs deleted file mode 100644 index ced2d3d..0000000 --- a/data/tests/src/lib.rs +++ /dev/null @@ -1,30 +0,0 @@ -use std::{env::current_dir, path::PathBuf}; - -use tokio::fs; - -#[cfg(test)] -pub mod test_vault_setup_and_member_register; - -#[cfg(test)] -pub mod test_virtual_file_creation_and_update; - -#[cfg(test)] -pub mod test_local_workspace_setup_and_account_management; - -#[cfg(test)] -pub mod test_sheet_creation_management_and_persistence; - -#[cfg(test)] -pub mod test_sheet_share_creation_and_management; - -pub async fn get_test_dir(area: &str) -> Result { - let dir = current_dir()?.join(".temp").join("test").join(area); - if !dir.exists() { - std::fs::create_dir_all(&dir)?; - } else { - // Regenerate existing directory - fs::remove_dir_all(&dir).await?; - fs::create_dir_all(&dir).await?; - } - Ok(dir) -} diff --git a/data/tests/src/test_local_workspace_setup_and_account_management.rs b/data/tests/src/test_local_workspace_setup_and_account_management.rs deleted file mode 100644 index edb9295..0000000 --- a/data/tests/src/test_local_workspace_setup_and_account_management.rs +++ /dev/null @@ -1,248 +0,0 @@ -use std::io::Error; - -use cfg_file::config::ConfigFile; -use vcs_data::{ - constants::{CLIENT_FILE_TODOLIST, CLIENT_FILE_WORKSPACE, USER_FILE_KEY, USER_FILE_MEMBER}, - data::{ - local::{LocalWorkspace, workspace_config::LocalConfig}, - member::Member, - user::UserDirectory, - }, -}; - -use crate::get_test_dir; - -#[tokio::test] -async fn test_local_workspace_setup_and_account_management() -> Result<(), std::io::Error> { - let dir = get_test_dir("local_workspace_account_management").await?; - - // Setup local workspace - LocalWorkspace::setup_local_workspace(dir.clone()).await?; - - // Check if the following files are created in `dir`: - // Files: CLIENT_FILE_WORKSPACE, CLIENT_FILE_README - assert!(dir.join(CLIENT_FILE_WORKSPACE).exists()); - assert!(dir.join(CLIENT_FILE_TODOLIST).exists()); - - // Get local workspace - let config = LocalConfig::read_from(dir.join(CLIENT_FILE_WORKSPACE)).await?; - let Some(_local_workspace) = LocalWorkspace::init(config, &dir) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "Local workspace not found!", - )); - }; - - // Create user directory from workspace path - let Some(user_directory) = UserDirectory::from_path(&dir) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "User directory not found!", - )); - }; - - // Test account registration - let member_id = "test_account"; - let member = Member::new(member_id); - - // Register account - user_directory.register_account(member.clone()).await?; - - // Check if the account config file exists - assert!( - dir.join(USER_FILE_MEMBER.replace("{self_id}", member_id)) - .exists() - ); - - // Test account retrieval - let retrieved_member = user_directory.account(&member_id.to_string()).await?; - assert_eq!(retrieved_member.id(), member.id()); - - // Test account IDs listing - let account_ids = user_directory.account_ids()?; - assert!(account_ids.contains(&member_id.to_string())); - - // Test accounts listing - let accounts = user_directory.accounts().await?; - assert_eq!(accounts.len(), 1); - assert_eq!(accounts[0].id(), member.id()); - - // Test account existence check - assert!(user_directory.account_cfg(&member_id.to_string()).is_some()); - - // Test private key check (should be false initially) - assert!(!user_directory.has_private_key(&member_id.to_string())); - - // Test account update - let mut updated_member = member.clone(); - updated_member.set_metadata("email", "test@example.com"); - user_directory - .update_account(updated_member.clone()) - .await?; - - // Verify update - let updated_retrieved = user_directory.account(&member_id.to_string()).await?; - assert_eq!( - updated_retrieved.metadata("email"), - Some(&"test@example.com".to_string()) - ); - - // Test account removal - user_directory.remove_account(&member_id.to_string())?; - - // Check if the account config file no longer exists - assert!( - !dir.join(USER_FILE_MEMBER.replace("{self_id}", member_id)) - .exists() - ); - - // Check if account is no longer in the list - let account_ids_after_removal = user_directory.account_ids()?; - assert!(!account_ids_after_removal.contains(&member_id.to_string())); - - Ok(()) -} - -#[tokio::test] -async fn test_account_private_key_management() -> Result<(), std::io::Error> { - let dir = get_test_dir("account_private_key_management").await?; - - // Create user directory - let Some(user_directory) = UserDirectory::from_path(&dir) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "User directory not found!", - )); - }; - - // Register account - let member_id = "test_account_with_key"; - let member = Member::new(member_id); - user_directory.register_account(member).await?; - - // Create a dummy private key file for testing - let private_key_path = dir.join(USER_FILE_KEY.replace("{self_id}", member_id)); - std::fs::create_dir_all(private_key_path.parent().unwrap())?; - std::fs::write(&private_key_path, "dummy_private_key_content")?; - - // Test private key existence check - assert!(user_directory.has_private_key(&member_id.to_string())); - - // Test private key path retrieval - assert!( - user_directory - .account_private_key(&member_id.to_string()) - .is_some() - ); - - // Remove account (should also remove private key) - user_directory.remove_account(&member_id.to_string())?; - - // Check if private key file is also removed - assert!(!private_key_path.exists()); - - Ok(()) -} - -#[tokio::test] -async fn test_multiple_account_management() -> Result<(), std::io::Error> { - let dir = get_test_dir("multiple_account_management").await?; - - // Create user directory - let Some(user_directory) = UserDirectory::from_path(&dir) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "User directory not found!", - )); - }; - - // Register multiple accounts - let account_names = vec!["alice", "bob", "charlie"]; - - for name in &account_names { - user_directory.register_account(Member::new(*name)).await?; - } - - // Test account IDs listing - let account_ids = user_directory.account_ids()?; - assert_eq!(account_ids.len(), 3); - - for name in &account_names { - assert!(account_ids.contains(&name.to_string())); - } - - // Test accounts listing - let accounts = user_directory.accounts().await?; - assert_eq!(accounts.len(), 3); - - // Remove one account - user_directory.remove_account(&"bob".to_string())?; - - // Verify removal - let account_ids_after_removal = user_directory.account_ids()?; - assert_eq!(account_ids_after_removal.len(), 2); - assert!(!account_ids_after_removal.contains(&"bob".to_string())); - assert!(account_ids_after_removal.contains(&"alice".to_string())); - assert!(account_ids_after_removal.contains(&"charlie".to_string())); - - Ok(()) -} - -#[tokio::test] -async fn test_account_registration_duplicate_prevention() -> Result<(), std::io::Error> { - let dir = get_test_dir("account_duplicate_prevention").await?; - - // Create user directory - let Some(user_directory) = UserDirectory::from_path(&dir) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "User directory not found!", - )); - }; - - // Register account - let member_id = "duplicate_test"; - user_directory - .register_account(Member::new(member_id)) - .await?; - - // Try to register same account again - should fail - let result = user_directory - .register_account(Member::new(member_id)) - .await; - assert!(result.is_err()); - - Ok(()) -} - -#[tokio::test] -async fn test_nonexistent_account_operations() -> Result<(), std::io::Error> { - let dir = get_test_dir("nonexistent_account_operations").await?; - - // Create user directory - let Some(user_directory) = UserDirectory::from_path(&dir) else { - return Err(Error::new( - std::io::ErrorKind::NotFound, - "User directory not found!", - )); - }; - - // Try to read non-existent account - should fail - let result = user_directory.account(&"nonexistent".to_string()).await; - assert!(result.is_err()); - - // Try to update non-existent account - should fail - let result = user_directory - .update_account(Member::new("nonexistent")) - .await; - assert!(result.is_err()); - - // Try to remove non-existent account - should succeed (idempotent) - let result = user_directory.remove_account(&"nonexistent".to_string()); - assert!(result.is_ok()); - - // Check private key for non-existent account - should be false - assert!(!user_directory.has_private_key(&"nonexistent".to_string())); - - Ok(()) -} diff --git a/data/tests/src/test_sheet_creation_management_and_persistence.rs b/data/tests/src/test_sheet_creation_management_and_persistence.rs deleted file mode 100644 index adff1bd..0000000 --- a/data/tests/src/test_sheet_creation_management_and_persistence.rs +++ /dev/null @@ -1,275 +0,0 @@ -use std::io::Error; - -use cfg_file::config::ConfigFile; -use vcs_data::{ - constants::{SERVER_FILE_SHEET, SERVER_FILE_VAULT}, - data::{ - member::{Member, MemberId}, - sheet::SheetName, - vault::{Vault, vault_config::VaultConfig, virtual_file::VirtualFileId}, - }, -}; - -use crate::get_test_dir; - -#[tokio::test] -async fn test_sheet_creation_management_and_persistence() -> Result<(), std::io::Error> { - let dir = get_test_dir("sheet_management").await?; - - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await?; - - // Get vault - let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; - let Some(vault) = Vault::init(config, &dir) else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); - }; - - // Add a member to use as sheet holder - let member_id: MemberId = "test_member".to_string(); - vault - .register_member_to_vault(Member::new(&member_id)) - .await?; - - // Test 1: Create a new sheet - let sheet_name: SheetName = "test_sheet".to_string(); - let sheet = vault.create_sheet(&sheet_name, &member_id).await?; - - // Verify sheet properties - assert_eq!(sheet.holder(), Some(&member_id)); - assert_eq!(sheet.holder(), Some(&member_id)); - assert!(sheet.mapping().is_empty()); - - // Verify sheet file was created - let sheet_path = dir.join(SERVER_FILE_SHEET.replace("{sheet_name}", &sheet_name)); - assert!(sheet_path.exists()); - - // Test 2: Add mapping entries to the sheet - let mut sheet = vault.sheet(&sheet_name).await?; - - // Add mapping entries for the files - let main_rs_path = vcs_data::data::sheet::SheetPathBuf::from("src/main.rs"); - let lib_rs_path = vcs_data::data::sheet::SheetPathBuf::from("src/lib.rs"); - let main_rs_id = VirtualFileId::new(); - let lib_rs_id = VirtualFileId::new(); - - sheet - .add_mapping( - main_rs_path.clone(), - main_rs_id.clone(), - "1.0.0".to_string(), - ) - .await?; - sheet - .add_mapping(lib_rs_path.clone(), lib_rs_id.clone(), "1.0.0".to_string()) - .await?; - - // Verify mappings were added - assert_eq!(sheet.mapping().len(), 2); - - // Test 3: Add more mapping entries - let mapping_path = vcs_data::data::sheet::SheetPathBuf::from("output/build.exe"); - let virtual_file_id = VirtualFileId::new(); - - sheet - .add_mapping( - mapping_path.clone(), - virtual_file_id.clone(), - "1.0.0".to_string(), - ) - .await?; - - // Verify mapping was added - assert_eq!(sheet.mapping().len(), 3); - assert_eq!( - sheet.mapping().get(&mapping_path).map(|meta| &meta.id), - Some(&virtual_file_id) - ); - - // Test 4: Persist sheet to disk - sheet.persist().await?; - - // Verify persistence by reloading the sheet - let reloaded_sheet = vault.sheet(&sheet_name).await?; - assert_eq!(reloaded_sheet.holder(), Some(&member_id)); - assert_eq!(reloaded_sheet.mapping().len(), 3); - - // Test 5: Remove mapping entry - let mut sheet_for_removal = vault.sheet(&sheet_name).await?; - let _removed_virtual_file_id = sheet_for_removal.remove_mapping(&mapping_path).await; - // Don't check the return value since it depends on virtual file existence - assert_eq!(sheet_for_removal.mapping().len(), 2); - - // Test 6: List all sheets in vault - let sheet_names = vault.sheet_names()?; - assert_eq!(sheet_names.len(), 2); - assert!(sheet_names.contains(&sheet_name)); - assert!(sheet_names.contains(&"ref".to_string())); - - let all_sheets = vault.sheets().await?; - assert_eq!(all_sheets.len(), 2); - // One sheet should be the test sheet, the other should be the ref sheet with host as holder - let test_sheet_holder = all_sheets - .iter() - .find(|s| s.holder() == Some(&member_id)) - .map(|s| s.holder()) - .unwrap(); - let ref_sheet_holder = all_sheets - .iter() - .find(|s| s.holder() == Some(&"host".to_string())) - .map(|s| s.holder()) - .unwrap(); - assert_eq!(test_sheet_holder, Some(&member_id)); - assert_eq!(ref_sheet_holder, Some(&"host".to_string())); - - // Test 7: Safe deletion (move to trash) - vault.delete_sheet_safely(&sheet_name).await?; - - // Verify sheet is not in normal listing but can be restored - let sheet_names_after_deletion = vault.sheet_names()?; - assert_eq!(sheet_names_after_deletion.len(), 1); - assert_eq!(sheet_names_after_deletion[0], "ref"); - - // Test 8: Restore sheet from trash - let restored_sheet = vault.sheet(&sheet_name).await?; - assert_eq!(restored_sheet.holder(), Some(&member_id)); - assert_eq!(restored_sheet.holder(), Some(&member_id)); - - // Verify sheet is back in normal listing - let sheet_names_after_restore = vault.sheet_names()?; - assert_eq!(sheet_names_after_restore.len(), 2); - assert!(sheet_names_after_restore.contains(&sheet_name)); - assert!(sheet_names_after_restore.contains(&"ref".to_string())); - - // Test 9: Permanent deletion - vault.delete_sheet(&sheet_name).await?; - - // Verify sheet is permanently gone - let sheet_names_final = vault.sheet_names()?; - assert_eq!(sheet_names_final.len(), 1); - assert_eq!(sheet_names_final[0], "ref"); - - // Attempt to access deleted sheet should fail - let result = vault.sheet(&sheet_name).await; - assert!(result.is_err()); - - // Clean up: Remove member - vault.remove_member_from_vault(&member_id)?; - - Ok(()) -} - -#[tokio::test] -async fn test_sheet_error_conditions() -> Result<(), std::io::Error> { - let dir = get_test_dir("sheet_error_conditions").await?; - - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await?; - - // Get vault - let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; - let Some(vault) = Vault::init(config, &dir) else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); - }; - - // Test 1: Create sheet with non-existent member should fail - let non_existent_member: MemberId = "non_existent_member".to_string(); - let sheet_name: SheetName = "test_sheet".to_string(); - - let result = vault.create_sheet(&sheet_name, &non_existent_member).await; - assert!(result.is_err()); - - // Add a member first - let member_id: MemberId = "test_member".to_string(); - vault - .register_member_to_vault(Member::new(&member_id)) - .await?; - - // Test 2: Create duplicate sheet should fail - vault.create_sheet(&sheet_name, &member_id).await?; - let result = vault.create_sheet(&sheet_name, &member_id).await; - assert!(result.is_err()); - - // Test 3: Delete non-existent sheet should fail - let non_existent_sheet: SheetName = "non_existent_sheet".to_string(); - let result = vault.delete_sheet(&non_existent_sheet).await; - assert!(result.is_err()); - - // Test 4: Safe delete non-existent sheet should fail - let result = vault.delete_sheet_safely(&non_existent_sheet).await; - assert!(result.is_err()); - - // Test 5: Restore non-existent sheet from trash should fail - let result = vault.restore_sheet(&non_existent_sheet).await; - assert!(result.is_err()); - - // Clean up - vault.remove_member_from_vault(&member_id)?; - - Ok(()) -} - -#[tokio::test] -async fn test_sheet_data_serialization() -> Result<(), std::io::Error> { - let dir = get_test_dir("sheet_serialization").await?; - - // Test serialization by creating a sheet through the vault - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await?; - - // Get vault - let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; - let Some(vault) = Vault::init(config, &dir) else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); - }; - - // Add a member - let member_id: MemberId = "test_member".to_string(); - vault - .register_member_to_vault(Member::new(&member_id)) - .await?; - - // Create a sheet - let sheet_name: SheetName = "test_serialization_sheet".to_string(); - let mut sheet = vault.create_sheet(&sheet_name, &member_id).await?; - - // Add some mappings - let main_rs_path = vcs_data::data::sheet::SheetPathBuf::from("src/main.rs"); - let lib_rs_path = vcs_data::data::sheet::SheetPathBuf::from("src/lib.rs"); - let main_rs_id = VirtualFileId::new(); - let lib_rs_id = VirtualFileId::new(); - - sheet - .add_mapping( - main_rs_path.clone(), - main_rs_id.clone(), - "1.0.0".to_string(), - ) - .await?; - sheet - .add_mapping(lib_rs_path.clone(), lib_rs_id.clone(), "1.0.0".to_string()) - .await?; - - // Add more mappings - let build_exe_id = VirtualFileId::new(); - - sheet - .add_mapping( - vcs_data::data::sheet::SheetPathBuf::from("output/build.exe"), - build_exe_id, - "1.0.0".to_string(), - ) - .await?; - - // Persist the sheet - sheet.persist().await?; - - // Verify the sheet file was created - let sheet_path = dir.join(SERVER_FILE_SHEET.replace("{sheet_name}", &sheet_name)); - assert!(sheet_path.exists()); - - // Clean up - vault.remove_member_from_vault(&member_id)?; - - Ok(()) -} diff --git a/data/tests/src/test_sheet_share_creation_and_management.rs b/data/tests/src/test_sheet_share_creation_and_management.rs deleted file mode 100644 index 5123360..0000000 --- a/data/tests/src/test_sheet_share_creation_and_management.rs +++ /dev/null @@ -1,631 +0,0 @@ -use std::io::Error; - -use cfg_file::config::ConfigFile; -use vcs_data::{ - constants::SERVER_FILE_VAULT, - data::{ - member::{Member, MemberId}, - sheet::{SheetName, SheetPathBuf}, - vault::{ - Vault, - mapping_share::{Share, ShareMergeMode, SheetShareId}, - vault_config::VaultConfig, - virtual_file::VirtualFileId, - }, - }, -}; - -use crate::get_test_dir; - -#[tokio::test] -async fn test_share_creation_and_retrieval() -> Result<(), std::io::Error> { - let dir = get_test_dir("share_creation").await?; - - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await?; - - // Get vault - let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; - let Some(vault) = Vault::init(config, &dir) else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); - }; - - // Add members - let sharer_id: MemberId = "sharer_member".to_string(); - let target_member_id: MemberId = "target_member".to_string(); - - vault - .register_member_to_vault(Member::new(&sharer_id)) - .await?; - vault - .register_member_to_vault(Member::new(&target_member_id)) - .await?; - - // Create source sheet for sharer - let source_sheet_name: SheetName = "source_sheet".to_string(); - let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; - - // Create target sheet for target member - let target_sheet_name: SheetName = "target_sheet".to_string(); - let _target_sheet = vault - .create_sheet(&target_sheet_name, &target_member_id) - .await?; - - // Add mappings to source sheet - let mut source_sheet = vault.sheet(&source_sheet_name).await?; - - let main_rs_path = SheetPathBuf::from("src/main.rs"); - let lib_rs_path = SheetPathBuf::from("src/lib.rs"); - let main_rs_id = VirtualFileId::from("main_rs_id_1"); - let lib_rs_id = VirtualFileId::from("lib_rs_id_1"); - - source_sheet - .add_mapping( - main_rs_path.clone(), - main_rs_id.clone(), - "1.0.0".to_string(), - ) - .await?; - source_sheet - .add_mapping(lib_rs_path.clone(), lib_rs_id.clone(), "1.0.0".to_string()) - .await?; - - // Persist source sheet - source_sheet.persist().await?; - - // Test 1: Share mappings from source sheet to target sheet - let description = "Test share of main.rs and lib.rs".to_string(); - // Need to get the sheet again after persist - let source_sheet = vault.sheet(&source_sheet_name).await?; - - source_sheet - .share_mappings( - &target_sheet_name, - vec![main_rs_path.clone(), lib_rs_path.clone()], - &sharer_id, - description.clone(), - ) - .await?; - - // Test 2: Get shares from target sheet - let target_sheet = vault.sheet(&target_sheet_name).await?; - - let shares = target_sheet.get_shares().await?; - - assert_eq!(shares.len(), 1, "Expected 1 share, found {}", shares.len()); - let share = &shares[0]; - - assert_eq!(share.sharer, sharer_id); - assert_eq!(share.description, description); - assert_eq!(share.from_sheet, source_sheet_name); - assert_eq!(share.mappings.len(), 2); - assert!(share.mappings.contains_key(&main_rs_path)); - assert!(share.mappings.contains_key(&lib_rs_path)); - assert!(share.path.is_some()); - - // Test 3: Get specific share by ID - let share_id = Share::gen_share_id(&sharer_id); - let _specific_share = target_sheet.get_share(&share_id).await; - - // Note: The share ID might not match exactly due to random generation, - // but we can verify the share exists by checking the shares list - assert!(shares.iter().any(|s| s.sharer == sharer_id)); - - // Clean up - vault.remove_member_from_vault(&sharer_id)?; - vault.remove_member_from_vault(&target_member_id)?; - - Ok(()) -} - -#[tokio::test] -async fn test_share_merge_modes() -> Result<(), std::io::Error> { - let dir = get_test_dir("share_merge_modes").await?; - - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await?; - - // Get vault - let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; - let Some(vault) = Vault::init(config, &dir) else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); - }; - - // Add members - let sharer_id: MemberId = "sharer".to_string(); - let target_member_id: MemberId = "target".to_string(); - - vault - .register_member_to_vault(Member::new(&sharer_id)) - .await?; - vault - .register_member_to_vault(Member::new(&target_member_id)) - .await?; - - // Create source and target sheets - let source_sheet_name: SheetName = "source".to_string(); - let target_sheet_name: SheetName = "target".to_string(); - - let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; - let _target_sheet = vault - .create_sheet(&target_sheet_name, &target_member_id) - .await?; - - // Add mappings to source sheet - let mut source_sheet = vault.sheet(&source_sheet_name).await?; - - let file1_path = SheetPathBuf::from("src/file1.rs"); - let file2_path = SheetPathBuf::from("src/file2.rs"); - let file1_id = VirtualFileId::from("file1_id_1"); - let file2_id = VirtualFileId::from("file2_id_1"); - - source_sheet - .add_mapping(file1_path.clone(), file1_id.clone(), "1.0.0".to_string()) - .await?; - source_sheet - .add_mapping(file2_path.clone(), file2_id.clone(), "1.0.0".to_string()) - .await?; - - source_sheet.persist().await?; - - // Share mappings - // Need to get the sheet again after persist - let source_sheet = vault.sheet(&source_sheet_name).await?; - source_sheet - .share_mappings( - &target_sheet_name, - vec![file1_path.clone(), file2_path.clone()], - &sharer_id, - "Test share".to_string(), - ) - .await?; - - // Get the share - let target_sheet = vault.sheet(&target_sheet_name).await?; - let shares = target_sheet.get_shares().await?; - assert_eq!(shares.len(), 1); - let share = shares[0].clone(); - - // Test 4: Safe mode merge (should succeed with no conflicts) - let result = target_sheet - .merge_share(share.clone(), ShareMergeMode::Safe) - .await; - - assert!( - result.is_ok(), - "Safe mode should succeed with no conflicts " - ); - - // Verify mappings were added to target sheet - let updated_target_sheet = vault.sheet(&target_sheet_name).await?; - assert_eq!(updated_target_sheet.mapping().len(), 2); - assert!(updated_target_sheet.mapping().contains_key(&file1_path)); - assert!(updated_target_sheet.mapping().contains_key(&file2_path)); - - // Clean up - vault.remove_member_from_vault(&sharer_id)?; - vault.remove_member_from_vault(&target_member_id)?; - - Ok(()) -} - -#[tokio::test] -async fn test_share_merge_conflicts() -> Result<(), std::io::Error> { - let dir = get_test_dir("share_conflicts").await?; - - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await?; - - // Get vault - let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; - let Some(vault) = Vault::init(config, &dir) else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); - }; - - // Add members - let sharer_id: MemberId = "sharer".to_string(); - let target_member_id: MemberId = "target".to_string(); - - vault - .register_member_to_vault(Member::new(&sharer_id)) - .await?; - vault - .register_member_to_vault(Member::new(&target_member_id)) - .await?; - - // Create source and target sheets - let source_sheet_name: SheetName = "source".to_string(); - let target_sheet_name: SheetName = "target".to_string(); - - let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; - let _target_sheet = vault - .create_sheet(&target_sheet_name, &target_member_id) - .await?; - - // Add conflicting mappings to both sheets - let mut source_sheet = vault.sheet(&source_sheet_name).await?; - let mut target_sheet_mut = vault.sheet(&target_sheet_name).await?; - - let conflicting_path = SheetPathBuf::from("src/conflicting.rs"); - let source_file_id = VirtualFileId::from("source_file_id_1"); - let target_file_id = VirtualFileId::from("target_file_id_1"); - - // Add same path with different IDs to both sheets (conflict) - source_sheet - .add_mapping( - conflicting_path.clone(), - source_file_id.clone(), - "1.0.0".to_string(), - ) - .await?; - - target_sheet_mut - .add_mapping( - conflicting_path.clone(), - target_file_id.clone(), - "1.0.0".to_string(), - ) - .await?; - - source_sheet.persist().await?; - target_sheet_mut.persist().await?; - - // Share the conflicting mapping - // Need to get the sheet again after persist - let source_sheet = vault.sheet(&source_sheet_name).await?; - source_sheet - .share_mappings( - &target_sheet_name, - vec![conflicting_path.clone()], - &sharer_id, - "Conflicting share".to_string(), - ) - .await?; - - // Get the share - let target_sheet = vault.sheet(&target_sheet_name).await?; - let shares = target_sheet.get_shares().await?; - assert_eq!(shares.len(), 1); - let share = shares[0].clone(); - - // Test 5: Safe mode merge with conflict (should fail) - let target_sheet_clone = vault.sheet(&target_sheet_name).await?; - let result = target_sheet_clone - .merge_share(share.clone(), ShareMergeMode::Safe) - .await; - - assert!(result.is_err(), "Safe mode should fail with conflicts"); - - // Test 6: Overwrite mode merge with conflict (should succeed) - let target_sheet_clone = vault.sheet(&target_sheet_name).await?; - let result = target_sheet_clone - .merge_share(share.clone(), ShareMergeMode::Overwrite) - .await; - - assert!( - result.is_ok(), - "Overwrite mode should succeed with conflicts" - ); - - // Verify the mapping was overwritten - let updated_target_sheet = vault.sheet(&target_sheet_name).await?; - let mapping = updated_target_sheet.mapping().get(&conflicting_path); - assert!(mapping.is_some()); - assert_eq!(mapping.unwrap().id, source_file_id); // Should be source's ID, not target's - - // Clean up - vault.remove_member_from_vault(&sharer_id)?; - vault.remove_member_from_vault(&target_member_id)?; - - Ok(()) -} - -#[tokio::test] -async fn test_share_skip_mode() -> Result<(), std::io::Error> { - let dir = get_test_dir("share_skip_mode").await?; - - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await?; - - // Get vault - let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; - let Some(vault) = Vault::init(config, &dir) else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); - }; - - // Add members - let sharer_id: MemberId = "sharer".to_string(); - let target_member_id: MemberId = "target".to_string(); - - vault - .register_member_to_vault(Member::new(&sharer_id)) - .await?; - vault - .register_member_to_vault(Member::new(&target_member_id)) - .await?; - - // Create source and target sheets - let source_sheet_name: SheetName = "source".to_string(); - let target_sheet_name: SheetName = "target".to_string(); - - let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; - let _target_sheet = vault - .create_sheet(&target_sheet_name, &target_member_id) - .await?; - - // Add mappings to both sheets - let mut source_sheet = vault.sheet(&source_sheet_name).await?; - let mut target_sheet_mut = vault.sheet(&target_sheet_name).await?; - - let conflicting_path = SheetPathBuf::from("src/conflicting.rs"); - let non_conflicting_path = SheetPathBuf::from("src/non_conflicting.rs"); - - let source_file_id = VirtualFileId::from("source_file_id_2"); - let target_file_id = VirtualFileId::from("target_file_id_2"); - let non_conflicting_id = VirtualFileId::from("non_conflicting_id_1"); - - // Add conflicting mapping to both sheets - source_sheet - .add_mapping( - conflicting_path.clone(), - source_file_id.clone(), - "1.0.0".to_string(), - ) - .await?; - - target_sheet_mut - .add_mapping( - conflicting_path.clone(), - target_file_id.clone(), - "1.0.0".to_string(), - ) - .await?; - - // Add non-conflicting mapping only to source - source_sheet - .add_mapping( - non_conflicting_path.clone(), - non_conflicting_id.clone(), - "1.0.0".to_string(), - ) - .await?; - - source_sheet.persist().await?; - target_sheet_mut.persist().await?; - - // Share both mappings - // Need to get the sheet again after persist - let source_sheet = vault.sheet(&source_sheet_name).await?; - source_sheet - .share_mappings( - &target_sheet_name, - vec![conflicting_path.clone(), non_conflicting_path.clone()], - &sharer_id, - "Mixed share".to_string(), - ) - .await?; - - // Get the share - let target_sheet = vault.sheet(&target_sheet_name).await?; - let shares = target_sheet.get_shares().await?; - assert_eq!(shares.len(), 1); - let share = shares[0].clone(); - - // Test 7: Skip mode merge with conflict (should skip conflicting, add non-conflicting) - let result = target_sheet - .merge_share(share.clone(), ShareMergeMode::Skip) - .await; - - assert!(result.is_ok(), "Skip mode should succeed"); - - // Verify only non-conflicting mapping was added - let updated_target_sheet = vault.sheet(&target_sheet_name).await?; - - // Conflicting mapping should still have target's ID - let conflicting_mapping = updated_target_sheet.mapping().get(&conflicting_path); - assert!(conflicting_mapping.is_some()); - assert_eq!(conflicting_mapping.unwrap().id, target_file_id); - - // Non-conflicting mapping should be added - let non_conflicting_mapping = updated_target_sheet.mapping().get(&non_conflicting_path); - assert!(non_conflicting_mapping.is_some()); - assert_eq!(non_conflicting_mapping.unwrap().id, non_conflicting_id); - - // Clean up - vault.remove_member_from_vault(&sharer_id)?; - vault.remove_member_from_vault(&target_member_id)?; - - Ok(()) -} - -#[tokio::test] -async fn test_share_removal() -> Result<(), std::io::Error> { - let dir = get_test_dir("share_removal").await?; - - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await?; - - // Get vault - let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; - let Some(vault) = Vault::init(config, &dir) else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); - }; - - // Add members - let sharer_id: MemberId = "sharer".to_string(); - let target_member_id: MemberId = "target".to_string(); - - vault - .register_member_to_vault(Member::new(&sharer_id)) - .await?; - vault - .register_member_to_vault(Member::new(&target_member_id)) - .await?; - - // Create source and target sheets - let source_sheet_name: SheetName = "source".to_string(); - let target_sheet_name: SheetName = "target".to_string(); - - let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; - let _target_sheet = vault - .create_sheet(&target_sheet_name, &target_member_id) - .await?; - - // Add mapping to source sheet - let mut source_sheet = vault.sheet(&source_sheet_name).await?; - - let file_path = SheetPathBuf::from("src/file.rs"); - let file_id = VirtualFileId::from("file_id_1"); - - source_sheet - .add_mapping(file_path.clone(), file_id.clone(), "1.0.0".to_string()) - .await?; - - source_sheet.persist().await?; - - // Need to get the sheet again after persist - let source_sheet = vault.sheet(&source_sheet_name).await?; - // Share mapping - source_sheet - .share_mappings( - &target_sheet_name, - vec![file_path.clone()], - &sharer_id, - "Test share for removal".to_string(), - ) - .await?; - - // Get the share - let target_sheet = vault.sheet(&target_sheet_name).await?; - let shares = target_sheet.get_shares().await?; - assert_eq!(shares.len(), 1); - let share = shares[0].clone(); - - // Test 8: Remove share - let result = share.remove().await; - - // Check if removal succeeded or failed gracefully - match result { - Ok(_) => { - // Share was successfully removed - let shares_after_removal = target_sheet.get_shares().await?; - assert_eq!(shares_after_removal.len(), 0); - } - Err((returned_share, _error)) => { - // Share removal failed, but we got the share backZ - // Error message may vary, just check that we got an error - // The share should be returned in the error - assert_eq!(returned_share.sharer, sharer_id); - } - } - - // Clean up - vault.remove_member_from_vault(&sharer_id)?; - vault.remove_member_from_vault(&target_member_id)?; - - Ok(()) -} - -#[tokio::test] -async fn test_share_error_conditions() -> Result<(), std::io::Error> { - let dir = get_test_dir("share_errors").await?; - - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await?; - - // Get vault - let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; - let Some(vault) = Vault::init(config, &dir) else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); - }; - - // Add member - let sharer_id: MemberId = "sharer".to_string(); - vault - .register_member_to_vault(Member::new(&sharer_id)) - .await?; - - // Create source sheet - let source_sheet_name: SheetName = "source".to_string(); - let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; - - // Add mapping to source sheet - let mut source_sheet = vault.sheet(&source_sheet_name).await?; - - let file_path = SheetPathBuf::from("src/file.rs"); - let file_id = VirtualFileId::from("file_id_2"); - - source_sheet - .add_mapping(file_path.clone(), file_id.clone(), "1.0.0".to_string()) - .await?; - - source_sheet.persist().await?; - - // Test 9: Share to non-existent sheet should fail - let non_existent_sheet: SheetName = "non_existent".to_string(); - // Need to get the sheet again after persist - let source_sheet = vault.sheet(&source_sheet_name).await?; - let result = source_sheet - .share_mappings( - &non_existent_sheet, - vec![file_path.clone()], - &sharer_id, - "Test".to_string(), - ) - .await; - - assert!(result.is_err()); - - // Test 10: Share non-existent mapping should fail - let target_sheet_name: SheetName = "target".to_string(); - let _target_sheet = vault.create_sheet(&target_sheet_name, &sharer_id).await?; - - let non_existent_path = SheetPathBuf::from("src/non_existent.rs"); - let result = source_sheet - .share_mappings( - &target_sheet_name, - vec![non_existent_path], - &sharer_id, - "Test".to_string(), - ) - .await; - - assert!(result.is_err()); - - // Test 11: Merge non-existent share should fail - let target_sheet = vault.sheet(&target_sheet_name).await?; - let non_existent_share_id: SheetShareId = "non_existent_share".to_string(); - let result = target_sheet - .merge_share_by_id(&non_existent_share_id, ShareMergeMode::Safe) - .await; - - assert!(result.is_err()); - - // Clean up - vault.remove_member_from_vault(&sharer_id)?; - - Ok(()) -} - -#[tokio::test] -async fn test_share_id_generation() -> Result<(), std::io::Error> { - // Test 12: Share ID generation - let sharer_id: MemberId = "test_sharer".to_string(); - - // Generate multiple IDs to ensure they're different - let id1 = Share::gen_share_id(&sharer_id); - let id2 = Share::gen_share_id(&sharer_id); - let id3 = Share::gen_share_id(&sharer_id); - - // IDs should be different due to random component - assert_ne!(id1, id2); - assert_ne!(id1, id3); - assert_ne!(id2, id3); - - // IDs should start with sharer name - assert!(id1.starts_with(&format!("test_sharer@"))); - assert!(id2.starts_with(&format!("test_sharer@"))); - assert!(id3.starts_with(&format!("test_sharer@"))); - - Ok(()) -} diff --git a/data/tests/src/test_vault_setup_and_member_register.rs b/data/tests/src/test_vault_setup_and_member_register.rs deleted file mode 100644 index d34e48e..0000000 --- a/data/tests/src/test_vault_setup_and_member_register.rs +++ /dev/null @@ -1,67 +0,0 @@ -use std::io::Error; - -use cfg_file::config::ConfigFile; -use vcs_data::{ - constants::{ - SERVER_FILE_MEMBER_INFO, SERVER_FILE_README, SERVER_FILE_VAULT, SERVER_PATH_MEMBER_PUB, - SERVER_PATH_MEMBERS, SERVER_PATH_SHEETS, SERVER_PATH_VF_ROOT, - }, - data::{ - member::Member, - vault::{Vault, vault_config::VaultConfig}, - }, -}; - -use crate::get_test_dir; - -#[tokio::test] -async fn test_vault_setup_and_member_register() -> Result<(), std::io::Error> { - let dir = get_test_dir("member_register").await?; - - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await?; - - // Check if the following files and directories are created in `dir`: - // Files: SERVER_FILE_VAULT, SERVER_FILE_README - // Directories: SERVER_PATH_SHEETS, - // SERVER_PATH_MEMBERS, - // SERVER_PATH_MEMBER_PUB, - // SERVER_PATH_VIRTUAL_FILE_ROOT - assert!(dir.join(SERVER_FILE_VAULT).exists()); - assert!(dir.join(SERVER_FILE_README).exists()); - assert!(dir.join(SERVER_PATH_SHEETS).exists()); - assert!(dir.join(SERVER_PATH_MEMBERS).exists()); - assert!(dir.join(SERVER_PATH_MEMBER_PUB).exists()); - assert!(dir.join(SERVER_PATH_VF_ROOT).exists()); - - // Get vault - let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; - let Some(vault) = Vault::init(config, &dir) else { - return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); - }; - - // Add member - let member_id = "test_member"; - vault - .register_member_to_vault(Member::new(member_id)) - .await?; - - const ID_PARAM: &str = "{member_id}"; - - // Check if the member info file exists - assert!( - dir.join(SERVER_FILE_MEMBER_INFO.replace(ID_PARAM, member_id)) - .exists() - ); - - // Remove member - vault.remove_member_from_vault(&member_id.to_string())?; - - // Check if the member info file not exists - assert!( - !dir.join(SERVER_FILE_MEMBER_INFO.replace(ID_PARAM, member_id)) - .exists() - ); - - Ok(()) -} diff --git a/data/tests/src/test_virtual_file_creation_and_update.rs b/data/tests/src/test_virtual_file_creation_and_update.rs deleted file mode 100644 index a09f7dc..0000000 --- a/data/tests/src/test_virtual_file_creation_and_update.rs +++ /dev/null @@ -1,162 +0,0 @@ -use std::time::Duration; - -use cfg_file::config::ConfigFile; -use tcp_connection_test::{ - handle::{ClientHandle, ServerHandle}, - target::TcpServerTarget, - target_configure::ServerTargetConfig, -}; -use tokio::{ - join, - time::{sleep, timeout}, -}; -use vcs_data::{ - constants::SERVER_FILE_VAULT, - data::{ - member::Member, - vault::{Vault, vault_config::VaultConfig, virtual_file::VirtualFileVersionDescription}, - }, -}; - -use crate::get_test_dir; - -struct VirtualFileCreateClientHandle; -struct VirtualFileCreateServerHandle; - -impl ClientHandle for VirtualFileCreateClientHandle { - async fn process(mut instance: tcp_connection::instance::ConnectionInstance) { - let dir = get_test_dir("virtual_file_creation_and_update_2") - .await - .unwrap(); - // Create first test file for virtual file creation - let test_content_1 = b"Test file content for virtual file creation"; - let temp_file_path_1 = dir.join("test_virtual_file_1.txt"); - - tokio::fs::write(&temp_file_path_1, test_content_1) - .await - .unwrap(); - - // Send the first file to server for virtual file creation - instance.write_file(&temp_file_path_1).await.unwrap(); - - // Create second test file for virtual file update - let test_content_2 = b"Updated test file content for virtual file"; - let temp_file_path_2 = dir.join("test_virtual_file_2.txt"); - - tokio::fs::write(&temp_file_path_2, test_content_2) - .await - .unwrap(); - - // Send the second file to server for virtual file update - instance.write_file(&temp_file_path_2).await.unwrap(); - } -} - -impl ServerHandle for VirtualFileCreateServerHandle { - async fn process(mut instance: tcp_connection::instance::ConnectionInstance) { - let dir = get_test_dir("virtual_file_creation_and_update") - .await - .unwrap(); - - // Setup vault - Vault::setup_vault(dir.clone(), "TestVault").await.unwrap(); - - // Read vault - let Some(vault) = Vault::init( - VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)) - .await - .unwrap(), - &dir, - ) else { - panic!("No vault found!"); - }; - - // Register member - let member_id = "test_member"; - vault - .register_member_to_vault(Member::new(member_id)) - .await - .unwrap(); - - // Create visual file - let virtual_file_id = vault - .create_virtual_file_from_connection(&mut instance, &member_id.to_string()) - .await - .unwrap(); - - // Grant edit right to member - vault - .grant_virtual_file_edit_right(&member_id.to_string(), &virtual_file_id) - .await - .unwrap(); - - // Update visual file - vault - .update_virtual_file_from_connection( - &mut instance, - &member_id.to_string(), - &virtual_file_id, - &"2".to_string(), - VirtualFileVersionDescription { - creator: member_id.to_string(), - description: "Update".to_string(), - }, - ) - .await - .unwrap(); - } -} - -#[tokio::test] -async fn test_virtual_file_creation_and_update() -> Result<(), std::io::Error> { - let host = "localhost:5009"; - - // Server setup - let Ok(server_target) = TcpServerTarget::< - VirtualFileCreateClientHandle, - VirtualFileCreateServerHandle, - >::from_domain(host) - .await - else { - panic!("Test target built failed from a domain named `{}`", host); - }; - - // Client setup - let Ok(client_target) = TcpServerTarget::< - VirtualFileCreateClientHandle, - VirtualFileCreateServerHandle, - >::from_domain(host) - .await - else { - panic!("Test target built failed from a domain named `{}`", host); - }; - - let future_server = async move { - // Only process once - let configured_server = server_target.server_cfg(ServerTargetConfig::default().once()); - - // Listen here - let _ = configured_server.listen().await; - }; - - let future_client = async move { - // Wait for server start - let _ = sleep(Duration::from_secs_f32(1.5)).await; - - // Connect here - let _ = client_target.connect().await; - }; - - let test_timeout = Duration::from_secs(15); - - timeout(test_timeout, async { join!(future_client, future_server) }) - .await - .map_err(|_| { - std::io::Error::new( - std::io::ErrorKind::TimedOut, - format!("Test timed out after {:?}", test_timeout), - ) - })?; - - Ok(()) -} diff --git a/examples/Cargo.toml b/examples/Cargo.toml deleted file mode 100644 index 12c52c1..0000000 --- a/examples/Cargo.toml +++ /dev/null @@ -1,25 +0,0 @@ -[package] -name = "examples" -edition = "2024" -version.workspace = true - -[[bin]] -name = "example_action_system" -path = "src/bin/example_action_system.rs" - -[dependencies] - -# Utils -tcp_connection = { path = "../utils/tcp_connection" } -cfg_file = { path = "../utils/cfg_file", features = ["default"] } -string_proc = { path = "../utils/string_proc" } - -# Core -action_system = { path = "../systems/action" } - -# Async & Networking -tokio = { version = "1.48.0", features = ["full"] } - -# Serialization -serde = { version = "1.0.228", features = ["derive"] } -serde_json = "1.0.145" diff --git a/examples/src/bin/example_action_system.rs b/examples/src/bin/example_action_system.rs deleted file mode 100644 index 776c982..0000000 --- a/examples/src/bin/example_action_system.rs +++ /dev/null @@ -1,18 +0,0 @@ -use action_system::{action::ActionContext, action_pool::ActionPool, macros::action_gen}; -use tcp_connection::error::TcpTargetError; - -#[tokio::main] -async fn main() { - let mut pool = ActionPool::new(); - register_print_name_action(&mut pool); - - proc_print_name_action(&pool, ActionContext::local(), "World".to_string()) - .await - .unwrap(); -} - -#[action_gen] -async fn print_name_action(_ctx: ActionContext, name: String) -> Result<(), TcpTargetError> { - println!("Hello, {}!", name); - Ok(()) -} diff --git a/examples/src/lib.rs b/examples/src/lib.rs deleted file mode 100644 index 8b13789..0000000 --- a/examples/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/legacy_actions/Cargo.toml b/legacy_actions/Cargo.toml new file mode 100644 index 0000000..a49e1b0 --- /dev/null +++ b/legacy_actions/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "vcs_actions" +edition = "2024" +version.workspace = true + +[dependencies] + +# Utils +tcp_connection = { path = "../utils/tcp_connection" } +cfg_file = { path = "../utils/cfg_file", features = ["default"] } +sha1_hash = { path = "../utils/sha1_hash" } +string_proc = { path = "../utils/string_proc" } + +# Core dependencies +action_system = { path = "../systems/action" } +vcs_data = { path = "../legacy_data" } + +# Error handling +thiserror = "2.0.17" + +# Serialization +serde = { version = "1.0.228", features = ["derive"] } +serde_json = "1.0.145" + +# Async & Networking +tokio = { version = "1.48.0", features = ["full"] } + +# Logging +log = "0.4.28" diff --git a/legacy_actions/src/connection.rs b/legacy_actions/src/connection.rs new file mode 100644 index 0000000..918f93c --- /dev/null +++ b/legacy_actions/src/connection.rs @@ -0,0 +1,3 @@ +pub mod action_service; +pub mod error; +pub mod protocol; diff --git a/legacy_actions/src/connection/action_service.rs b/legacy_actions/src/connection/action_service.rs new file mode 100644 index 0000000..3e05a70 --- /dev/null +++ b/legacy_actions/src/connection/action_service.rs @@ -0,0 +1,221 @@ +use std::{ + env::set_current_dir, + net::SocketAddr, + path::PathBuf, + sync::Arc, + time::{Duration, Instant}, +}; + +use action_system::{action::ActionContext, action_pool::ActionPool}; +use cfg_file::config::ConfigFile; +use log::{debug, error, info, warn}; +use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance}; +use tokio::{ + net::{TcpListener, TcpStream}, + select, signal, spawn, + sync::mpsc, +}; +use vcs_data::data::vault::{Vault, vault_config::VaultConfig}; + +use crate::{ + connection::protocol::RemoteActionInvoke, registry::server_registry::server_action_pool, +}; + +// Start the server with a Vault using the specified directory +pub async fn server_entry( + vault_path: impl Into, + port_override: u16, +) -> Result<(), TcpTargetError> { + let vault_path = vault_path.into(); + + // Set to vault path + set_current_dir(&vault_path).map_err(|e| TcpTargetError::Io(e.to_string()))?; + + // Read the vault cfg + let vault_cfg = VaultConfig::read().await?; + + // Create TCPListener + let listener = create_tcp_listener(&vault_cfg, port_override).await?; + + // Initialize the vault + let vault: Arc = init_vault(vault_cfg, vault_path).await?; + + // Lock the vault + vault + .lock() + .map_err(|e| TcpTargetError::Locked(e.to_string()))?; + + // Create ActionPool + let action_pool: Arc = Arc::new(server_action_pool()); + + // Start the server + let (_shutdown_rx, future) = build_server_future(vault.clone(), action_pool.clone(), listener); + future.await?; // Start and block until shutdown + + // Unlock the vault + vault.unlock()?; + + Ok(()) +} + +async fn create_tcp_listener( + cfg: &VaultConfig, + port_override: u16, +) -> Result { + let local_bind_addr = cfg.server_config().local_bind(); + let port = if port_override > 0 { + port_override // Override -> PORT > 0 + } else { + cfg.server_config().port() // Default -> Port = 0 + }; + let bind_port = port; + let sock_addr = SocketAddr::new(*local_bind_addr, bind_port); + let listener = TcpListener::bind(sock_addr).await?; + + Ok(listener) +} + +async fn init_vault(cfg: VaultConfig, path: PathBuf) -> Result, TcpTargetError> { + // Init and create the vault + let Some(vault) = Vault::init(cfg, path) else { + return Err(TcpTargetError::NotFound("Vault not found".to_string())); + }; + let vault: Arc = Arc::new(vault); + + Ok(vault) +} + +fn build_server_future( + vault: Arc, + action_pool: Arc, + listener: TcpListener, +) -> ( + mpsc::Sender<()>, + impl std::future::Future>, +) { + let (tx, mut rx) = mpsc::channel::(100); + let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(1); + let mut active_connections = 0; + let mut shutdown_requested = false; + + // Spawn task to handle Ctrl+C with rapid exit detection + let shutdown_tx_clone = shutdown_tx.clone(); + spawn(async move { + let mut ctrl_c_count = 0; + let mut last_ctrl_c_time = Instant::now(); + + while let Ok(()) = signal::ctrl_c().await { + let now = Instant::now(); + + // Reset counter if more than 5 seconds have passed + if now.duration_since(last_ctrl_c_time) > Duration::from_secs(5) { + ctrl_c_count = 0; + } + + ctrl_c_count += 1; + last_ctrl_c_time = now; + + let _ = shutdown_tx_clone.send(()).await; + + // If 3 Ctrl+C within 5 seconds, exit immediately + if ctrl_c_count >= 3 { + info!("Shutdown. (3/3)"); + std::process::exit(0); + } else { + info!("Ctrl + C to force shutdown. ({} / 3)", ctrl_c_count); + } + } + }); + + let future = async move { + loop { + select! { + // Accept new connections + accept_result = listener.accept(), if !shutdown_requested => { + match accept_result { + Ok((stream, _addr)) => { + debug!("New connection. (now {})", active_connections); + let _ = tx.send(1).await; + + let vault_clone = vault.clone(); + let action_pool_clone = action_pool.clone(); + let tx_clone = tx.clone(); + + spawn(async move { + process_connection(stream, vault_clone, action_pool_clone).await; + debug!("A connection closed. (now {})", active_connections); + let _ = tx_clone.send(-1).await; + }); + } + Err(_) => { + continue; + } + } + } + + // Handle connection count updates + Some(count_change) = rx.recv() => { + active_connections = (active_connections as i32 + count_change) as usize; + + // Check if we should shutdown after all connections are done + if shutdown_requested && active_connections == 0 { + break; + } + } + + // Handle shutdown signal + _ = shutdown_rx.recv() => { + shutdown_requested = true; + // If no active connections, break immediately + if active_connections == 0 { + info!("No active connections. Shutting down."); + break; + } else { + warn!("Cannot shutdown while active connections exist! ({} active)", active_connections); + } + } + } + } + + Ok(()) + }; + + (shutdown_tx, future) +} + +async fn process_connection(stream: TcpStream, vault: Arc, action_pool: Arc) { + // Setup connection instance + let mut instance = ConnectionInstance::from(stream); + + // Read action name and action arguments + let msg = match instance.read_msgpack::().await { + Ok(msg) => msg, + Err(e) => { + error!("Failed to read action message: {}", e); + return; + } + }; + + // Build context + let ctx: ActionContext = ActionContext::remote().insert_instance(instance); + + // Insert vault into context + let ctx = ctx.with_arc_data(vault); + + info!( + "Process action `{}` with argument `{}`", + msg.action_name, msg.action_args_json + ); + + // Process action + let result = action_pool + .process_json(&msg.action_name, ctx, msg.action_args_json) + .await; + + match result { + Ok(_result_json) => {} + Err(e) => { + warn!("Failed to process action `{}`: {}", msg.action_name, e); + } + } +} diff --git a/legacy_actions/src/connection/error.rs b/legacy_actions/src/connection/error.rs new file mode 100644 index 0000000..1a4e221 --- /dev/null +++ b/legacy_actions/src/connection/error.rs @@ -0,0 +1,27 @@ +use std::io; +use thiserror::Error; +use vcs_data::data::member::MemberId; + +#[derive(Error, Debug, Clone)] +pub enum ConnectionError { + #[error("I/O error: {0}")] + Io(String), +} + +#[derive(Error, Debug, Clone)] +pub enum ProcessActionError { + #[error("Action `{0}` not registered")] + ActionNotRegistered(String), + + #[error("Authorize `{0}` failed")] + AuthorizeFailed(MemberId), + + #[error("Authorize host `{0}` failed")] + AuthorizeHostFailed(MemberId), +} + +impl From for ConnectionError { + fn from(error: io::Error) -> Self { + ConnectionError::Io(error.to_string()) + } +} diff --git a/legacy_actions/src/connection/protocol.rs b/legacy_actions/src/connection/protocol.rs new file mode 100644 index 0000000..2cebe79 --- /dev/null +++ b/legacy_actions/src/connection/protocol.rs @@ -0,0 +1,7 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Default, Clone, Serialize, Deserialize)] +pub struct RemoteActionInvoke { + pub action_name: String, + pub action_args_json: String, +} diff --git a/legacy_actions/src/lib.rs b/legacy_actions/src/lib.rs new file mode 100644 index 0000000..c1dda86 --- /dev/null +++ b/legacy_actions/src/lib.rs @@ -0,0 +1,4 @@ +pub mod connection; +pub mod local_actions; +pub mod registry; +pub mod remote_actions; diff --git a/legacy_actions/src/local_actions.rs b/legacy_actions/src/local_actions.rs new file mode 100644 index 0000000..d6f47f9 --- /dev/null +++ b/legacy_actions/src/local_actions.rs @@ -0,0 +1,2 @@ +pub mod account_manage; +pub mod current_sheet; diff --git a/legacy_actions/src/local_actions/account_manage.rs b/legacy_actions/src/local_actions/account_manage.rs new file mode 100644 index 0000000..03a7851 --- /dev/null +++ b/legacy_actions/src/local_actions/account_manage.rs @@ -0,0 +1,3 @@ +pub mod register_account; +pub mod remove_account; +pub mod switch_account; diff --git a/legacy_actions/src/local_actions/account_manage/register_account.rs b/legacy_actions/src/local_actions/account_manage/register_account.rs new file mode 100644 index 0000000..e69de29 diff --git a/legacy_actions/src/local_actions/account_manage/remove_account.rs b/legacy_actions/src/local_actions/account_manage/remove_account.rs new file mode 100644 index 0000000..e69de29 diff --git a/legacy_actions/src/local_actions/account_manage/switch_account.rs b/legacy_actions/src/local_actions/account_manage/switch_account.rs new file mode 100644 index 0000000..e69de29 diff --git a/legacy_actions/src/local_actions/current_sheet.rs b/legacy_actions/src/local_actions/current_sheet.rs new file mode 100644 index 0000000..785d7ee --- /dev/null +++ b/legacy_actions/src/local_actions/current_sheet.rs @@ -0,0 +1,2 @@ +pub mod exit_sheet; +pub mod use_sheet; diff --git a/legacy_actions/src/local_actions/current_sheet/exit_sheet.rs b/legacy_actions/src/local_actions/current_sheet/exit_sheet.rs new file mode 100644 index 0000000..e69de29 diff --git a/legacy_actions/src/local_actions/current_sheet/use_sheet.rs b/legacy_actions/src/local_actions/current_sheet/use_sheet.rs new file mode 100644 index 0000000..e69de29 diff --git a/legacy_actions/src/registry.rs b/legacy_actions/src/registry.rs new file mode 100644 index 0000000..ceec1a1 --- /dev/null +++ b/legacy_actions/src/registry.rs @@ -0,0 +1,2 @@ +pub mod client_registry; +pub mod server_registry; diff --git a/legacy_actions/src/registry/client_registry.rs b/legacy_actions/src/registry/client_registry.rs new file mode 100644 index 0000000..db69889 --- /dev/null +++ b/legacy_actions/src/registry/client_registry.rs @@ -0,0 +1,128 @@ +use std::sync::Arc; + +use action_system::{action::ActionContext, action_pool::ActionPool}; +use cfg_file::config::ConfigFile; +use tcp_connection::error::TcpTargetError; +use vcs_data::data::{ + local::{LocalWorkspace, workspace_config::LocalConfig}, + user::UserDirectory, +}; + +use crate::{ + connection::protocol::RemoteActionInvoke, + remote_actions::{ + content_manage::track_file::register_track_file_action, + edit_right_manage::change_virtual_file_edit_right::register_change_virtual_file_edit_right_action, + mapping_manage::{ + edit_mapping::register_edit_mapping_action, + merge_share_mapping::register_merge_share_mapping_action, + share_mapping::register_share_mapping_action, + }, + sheet_manage::{ + drop_sheet::register_drop_sheet_action, make_sheet::register_make_sheet_action, + }, + workspace_manage::{ + set_upstream_vault::register_set_upstream_vault_action, + update_to_latest_info::register_update_to_latest_info_action, + }, + }, +}; + +fn register_actions(pool: &mut ActionPool) { + // Pool register here + + // Local Actions + register_set_upstream_vault_action(pool); + register_update_to_latest_info_action(pool); + + // Sheet Actions + register_make_sheet_action(pool); + register_drop_sheet_action(pool); + register_edit_mapping_action(pool); + + // Share / Merge Share Actions + register_share_mapping_action(pool); + register_merge_share_mapping_action(pool); + + // Track Action + register_track_file_action(pool); + + // User Actions + register_change_virtual_file_edit_right_action(pool); +} + +pub fn client_action_pool() -> ActionPool { + // Create pool + let mut pool = ActionPool::new(); + + // Register actions + register_actions(&mut pool); + + // Add process events + pool.set_on_proc_begin(|ctx, args| Box::pin(on_proc_begin(ctx, args))); + + // Return + pool +} + +async fn on_proc_begin( + ctx: &mut ActionContext, + _args: &(dyn std::any::Any + Send + Sync), +) -> Result<(), TcpTargetError> { + // Is ctx remote + let is_remote = ctx.is_remote_action(); + + // Action name and arguments + let action_name = ctx.action_name().to_string(); + let action_args_json = ctx.action_args_json().clone(); + + // Insert LocalWorkspace Arc + let Ok(local_config) = LocalConfig::read().await else { + return Err(TcpTargetError::NotFound( + "The current directory does not have a local workspace".to_string(), + )); + }; + let local_workspace = match LocalWorkspace::init_current_dir(local_config) { + Some(workspace) => workspace, + None => { + return Err(TcpTargetError::NotFound( + "Failed to initialize local workspace.".to_string(), + )); + } + }; + let local_workspace_arc = Arc::new(local_workspace); + ctx.insert_arc_data(local_workspace_arc); + + // Insert UserDirectory Arc + let Some(user_directory) = UserDirectory::current_cfg_dir() else { + return Err(TcpTargetError::NotFound( + "The user directory does not exist.".to_string(), + )); + }; + + let user_directory_arc = Arc::new(user_directory); + ctx.insert_arc_data(user_directory_arc); + + // Get instance + let Some(instance) = ctx.instance() else { + return Err(TcpTargetError::Unsupported( + "Missing ConnectionInstance in current context, this ActionPool does not support this call" + .to_string())); + }; + + // If it's remote, invoke action at server + if is_remote { + // Build protocol message + let msg = RemoteActionInvoke { + action_name, + action_args_json, + }; + + // Send + let mut instance = instance.lock().await; + instance.write_msgpack(&msg).await?; + } + + // Return OK, wait for client to execute Action locally + Ok(()) +} diff --git a/legacy_actions/src/registry/server_registry.rs b/legacy_actions/src/registry/server_registry.rs new file mode 100644 index 0000000..aee867c --- /dev/null +++ b/legacy_actions/src/registry/server_registry.rs @@ -0,0 +1,43 @@ +use action_system::action_pool::ActionPool; + +use crate::remote_actions::{ + content_manage::track_file::register_track_file_action, + edit_right_manage::change_virtual_file_edit_right::register_change_virtual_file_edit_right_action, + mapping_manage::{ + edit_mapping::register_edit_mapping_action, + merge_share_mapping::register_merge_share_mapping_action, + share_mapping::register_share_mapping_action, + }, + sheet_manage::{ + drop_sheet::register_drop_sheet_action, make_sheet::register_make_sheet_action, + }, + workspace_manage::{ + set_upstream_vault::register_set_upstream_vault_action, + update_to_latest_info::register_update_to_latest_info_action, + }, +}; + +pub fn server_action_pool() -> ActionPool { + let mut pool = ActionPool::new(); + + // Local Actions + register_set_upstream_vault_action(&mut pool); + register_update_to_latest_info_action(&mut pool); + + // Sheet Actions + register_make_sheet_action(&mut pool); + register_drop_sheet_action(&mut pool); + register_edit_mapping_action(&mut pool); + + // Share / Merge Share Actions + register_share_mapping_action(&mut pool); + register_merge_share_mapping_action(&mut pool); + + // Track Action + register_track_file_action(&mut pool); + + // User Actions + register_change_virtual_file_edit_right_action(&mut pool); + + pool +} diff --git a/legacy_actions/src/remote_actions.rs b/legacy_actions/src/remote_actions.rs new file mode 100644 index 0000000..d15edc9 --- /dev/null +++ b/legacy_actions/src/remote_actions.rs @@ -0,0 +1,288 @@ +use std::sync::Arc; + +use action_system::action::ActionContext; +use cfg_file::config::ConfigFile; +use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance}; +use tokio::sync::{Mutex, mpsc::Sender}; +use vcs_data::{ + constants::{SERVER_PATH_MEMBER_PUB, VAULT_HOST_NAME}, + data::{ + local::{LocalWorkspace, latest_info::LatestInfo, workspace_config::LocalConfig}, + member::MemberId, + sheet::SheetName, + user::UserDirectory, + vault::Vault, + }, +}; + +pub mod content_manage; +pub mod edit_right_manage; +pub mod mapping_manage; +pub mod sheet_manage; +pub mod workspace_manage; + +/// Check if the connection instance is valid in the given context. +/// This function is used to verify the connection instance in actions that require remote calls. +pub fn check_connection_instance( + ctx: &ActionContext, +) -> Result<&Arc>, TcpTargetError> { + let Some(instance) = ctx.instance() else { + return Err(TcpTargetError::NotFound( + "Connection instance lost.".to_string(), + )); + }; + Ok(instance) +} + +/// Try to get the Vault instance from the context. +pub fn try_get_vault(ctx: &ActionContext) -> Result, TcpTargetError> { + let Some(vault) = ctx.get_arc::() else { + return Err(TcpTargetError::NotFound( + "Vault instance not found".to_string(), + )); + }; + Ok(vault) +} + +/// Try to get the LocalWorkspace instance from the context. +pub fn try_get_local_workspace(ctx: &ActionContext) -> Result, TcpTargetError> { + let Some(local_workspace) = ctx.get_arc::() else { + return Err(TcpTargetError::NotFound( + "LocalWorkspace instance not found".to_string(), + )); + }; + Ok(local_workspace) +} + +/// Try to get the UserDirectory instance from the context. +pub fn try_get_user_directory(ctx: &ActionContext) -> Result, TcpTargetError> { + let Some(user_directory) = ctx.get_arc::() else { + return Err(TcpTargetError::NotFound( + "UserDirectory instance not found".to_string(), + )); + }; + Ok(user_directory) +} + +/// Try to get the LocalWorkspace instance from the context. +pub fn try_get_local_output(ctx: &ActionContext) -> Result>, TcpTargetError> { + let Some(output) = ctx.get_arc::>() else { + return Err(TcpTargetError::NotFound( + "Client sender not found".to_string(), + )); + }; + Ok(output) +} + +/// Authenticate member based on context and return MemberId +pub async fn auth_member( + ctx: &ActionContext, + instance: &Arc>, +) -> Result<(MemberId, bool), TcpTargetError> { + // Window开服Linux连接 -> 此函数内产生 early eof + // ~ WS # jv update + // 身份认证失败:I/O error: early eof! + + // 分析相应流程: + // 1. 服务端发起挑战,客户端接受 + // 2. 服务端发送结果,客户端接受 + // 3. 推测此时发生 early eof ---> 无 ack,导致客户端尝试拿到结果时,服务端已经结束 + // 这很有可能是 Windows 和 Linux 对于连接处理的方案差异导致的问题,需要进一步排查 + + // Start Challenge (Remote) + if ctx.is_proc_on_remote() { + let mut mut_instance = instance.lock().await; + let vault = try_get_vault(ctx)?; + + let using_host_mode = mut_instance.read_msgpack::().await?; + + let result = mut_instance + .challenge(vault.vault_path().join(SERVER_PATH_MEMBER_PUB)) + .await; + + return match result { + Ok((pass, member_id)) => { + if !pass { + // Send false to inform the client that authentication failed + mut_instance.write(false).await?; + Err(TcpTargetError::Authentication( + "Authenticate failed.".to_string(), + )) + } else { + if using_host_mode { + if vault.config().vault_host_list().contains(&member_id) { + // Using Host mode authentication, and is indeed an administrator + mut_instance.write(true).await?; + Ok((member_id, true)) + } else { + // Using Host mode authentication, but not an administrator + mut_instance.write(false).await?; + Err(TcpTargetError::Authentication( + "Authenticate failed.".to_string(), + )) + } + } else { + // Not using Host mode authentication + mut_instance.write(true).await?; + Ok((member_id, false)) + } + } + } + Err(e) => Err(e), + }; + } + + // Accept Challenge (Local) + if ctx.is_proc_on_local() { + let mut mut_instance = instance.lock().await; + let local_workspace = try_get_local_workspace(ctx)?; + let (is_host_mode, member_name) = { + let cfg = local_workspace.config().lock_owned().await; + (cfg.is_host_mode(), cfg.current_account()) + }; + let user_directory = try_get_user_directory(ctx)?; + + // Inform remote whether to authenticate in Host mode + mut_instance.write_msgpack(is_host_mode).await?; + + // Member name & Private key + let private_key = user_directory.account_private_key_path(&member_name); + let _ = mut_instance + .accept_challenge(private_key, &member_name) + .await?; + + // Read result + let challenge_result = mut_instance.read::().await?; + if challenge_result { + return Ok((member_name.clone(), is_host_mode)); + } else { + return Err(TcpTargetError::Authentication( + "Authenticate failed.".to_string(), + )); + } + } + + Err(TcpTargetError::NoResult("Auth failed.".to_string())) +} + +/// Get the current sheet name based on the context (local or remote). +/// This function handles the communication between local and remote instances +/// to verify and retrieve the current sheet name and whether it's a reference sheet. +/// +/// On local: +/// - Reads the current sheet from local configuration +/// - Sends the sheet name to remote for verification +/// - Returns the sheet name and whether it's a reference sheet if remote confirms it exists +/// +/// On remote: +/// - Receives sheet name from local +/// - Verifies the sheet exists in the vault +/// - Checks if the sheet is a reference sheet +/// - If allow_ref is true, reference sheets are allowed to pass verification +/// - Sends confirmation and reference status back to local +/// +/// Returns a tuple of (SheetName, bool) where the bool indicates if it's a reference sheet, +/// or an error if the sheet doesn't exist or doesn't meet the verification criteria. +pub async fn get_current_sheet_name( + ctx: &ActionContext, + instance: &Arc>, + member_id: &MemberId, + allow_ref: bool, +) -> Result<(SheetName, bool), TcpTargetError> { + let mut mut_instance = instance.lock().await; + if ctx.is_proc_on_local() { + let workspace = try_get_local_workspace(ctx)?; + let config = LocalConfig::read().await?; + let latest = LatestInfo::read_from(LatestInfo::latest_info_path( + workspace.local_path(), + member_id, + )) + .await?; + if let Some(sheet_name) = config.sheet_in_use() { + // Send sheet name + mut_instance.write_msgpack(sheet_name).await?; + + // Read result + if mut_instance.read_msgpack::().await? { + // Check if sheet is a reference sheet + let is_ref_sheet = latest.reference_sheets.contains(sheet_name); + if allow_ref { + // Allow reference sheets, directly return the determination result + return Ok((sheet_name.clone(), is_ref_sheet)); + } else if is_ref_sheet { + // Not allowed but it's a reference sheet, return an error + return Err(TcpTargetError::ReferenceSheetNotAllowed( + "Reference sheet not allowed".to_string(), + )); + } else { + // Not allowed but not a reference sheet, return normally + return Ok((sheet_name.clone(), false)); + } + } else { + return Err(TcpTargetError::NotFound("Sheet not found".to_string())); + } + } + // Send empty sheet_name + mut_instance.write_msgpack("".to_string()).await?; + + // Read result, since we know it's impossible to pass here, we just consume this result + let _ = mut_instance.read_msgpack::().await?; + + return Err(TcpTargetError::NotFound("Sheet not found".to_string())); + } + if ctx.is_proc_on_remote() { + let vault = try_get_vault(ctx)?; + + // Read sheet name + let sheet_name: SheetName = mut_instance.read_msgpack().await?; + + // Check if sheet exists + if let Ok(sheet) = vault.sheet(&sheet_name).await + && let Some(holder) = sheet.holder() + { + let is_ref_sheet = holder == VAULT_HOST_NAME; + if allow_ref { + // Allow reference sheets, directly return the determination result + if holder == member_id || holder == VAULT_HOST_NAME { + mut_instance.write_msgpack(true).await?; + return Ok((sheet.name().clone(), is_ref_sheet)); + } + } else if is_ref_sheet { + // Not allowed but it's a reference sheet, return an error + mut_instance.write_msgpack(true).await?; + return Err(TcpTargetError::ReferenceSheetNotAllowed( + "Reference sheet not allowed".to_string(), + )); + } else { + // Not allowed but not a reference sheet, return normally + if holder == member_id { + mut_instance.write_msgpack(true).await?; + return Ok((sheet_name.clone(), false)); + } + } + } + // Tell local the check is not passed + mut_instance.write_msgpack(false).await?; + return Err(TcpTargetError::NotFound("Sheet not found".to_string())); + } + Err(TcpTargetError::NoResult("NoResult".to_string())) +} + +/// The macro to write and return a result. +#[macro_export] +macro_rules! write_and_return { + ($instance:expr, $result:expr) => {{ + $instance.lock().await.write($result).await?; + return Ok($result); + }}; +} + +/// The macro to send formatted string to output channel. +/// Usage: local_println!(output, "format string", arg1, arg2, ...) +#[macro_export] +macro_rules! local_println { + ($output:expr, $($arg:tt)*) => {{ + let formatted = format!($($arg)*); + let _ = $output.send(formatted).await; + }}; +} diff --git a/legacy_actions/src/remote_actions/content_manage.rs b/legacy_actions/src/remote_actions/content_manage.rs new file mode 100644 index 0000000..1568085 --- /dev/null +++ b/legacy_actions/src/remote_actions/content_manage.rs @@ -0,0 +1 @@ +pub mod track_file; diff --git a/legacy_actions/src/remote_actions/content_manage/track_file.rs b/legacy_actions/src/remote_actions/content_manage/track_file.rs new file mode 100644 index 0000000..a59ca76 --- /dev/null +++ b/legacy_actions/src/remote_actions/content_manage/track_file.rs @@ -0,0 +1,985 @@ +use std::{ + collections::{HashMap, HashSet}, + path::PathBuf, + sync::Arc, + time::SystemTime, +}; + +use action_system::{action::ActionContext, macros::action_gen}; +use cfg_file::config::ConfigFile; +use serde::{Deserialize, Serialize}; +use sha1_hash::calc_sha1; +use tcp_connection::{error::TcpTargetError, instance::ConnectionInstance}; +use tokio::{fs, sync::Mutex}; +use vcs_data::{ + constants::{CLIENT_FILE_TEMP_FILE, KEY_TEMP_NAME}, + data::{ + local::{ + cached_sheet::CachedSheet, latest_file_data::LatestFileData, + local_sheet::LocalMappingMetadata, modified_status::sign_vault_modified, + workspace_analyzer::AnalyzeResult, + }, + member::MemberId, + sheet::SheetName, + vault::{ + vault_config::VaultUuid, + virtual_file::{VirtualFileId, VirtualFileVersion, VirtualFileVersionDescription}, + }, + }, +}; + +use crate::{ + local_println, + remote_actions::{ + auth_member, check_connection_instance, get_current_sheet_name, try_get_local_output, + try_get_local_workspace, try_get_vault, + }, +}; + +pub type NextVersion = String; +pub type UpdateDescription = String; + +#[derive(Serialize, Deserialize)] +pub struct TrackFileActionArguments { + // Path need to track + pub relative_pathes: HashSet, + + // File update info + pub file_update_info: HashMap, + + // Print infos + pub print_infos: bool, + + // overwrite modified files + pub allow_overwrite_modified: bool, +} + +#[derive(Serialize, Deserialize)] +pub enum TrackFileActionResult { + Done { + created: Vec, + updated: Vec, + synced: Vec, + skipped: Vec, + }, + + // Fail + AuthorizeFailed(String), + + /// There are local move or missing items that have not been resolved, + /// this situation does not allow track + StructureChangesNotSolved, + + CreateTaskFailed(CreateTaskResult), + UpdateTaskFailed(UpdateTaskResult), + SyncTaskFailed(SyncTaskResult), +} + +#[derive(Serialize, Deserialize)] +pub enum CreateTaskResult { + Success(Vec), // Success(success_relative_pathes) + + /// Create file on existing path in the sheet + CreateFileOnExistPath(PathBuf), + + /// Sheet not found + SheetNotFound(SheetName), +} + +#[derive(Serialize, Deserialize)] +pub enum UpdateTaskResult { + Success(Vec), // Success(success_relative_pathes) + + VerifyFailed { + path: PathBuf, + reason: VerifyFailReason, + }, +} + +#[derive(Serialize, Deserialize, Clone)] +pub enum VerifyFailReason { + SheetNotFound(SheetName), + MappingNotFound, + VirtualFileNotFound(VirtualFileId), + VirtualFileReadFailed(VirtualFileId), + NotHeld, + VersionDismatch(VirtualFileVersion, VirtualFileVersion), // (CurrentVersion, RemoteVersion) + UpdateButNoDescription, // File needs update, but no description exists + VersionAlreadyExist(VirtualFileVersion), // (RemoteVersion) +} + +#[derive(Serialize, Deserialize)] +pub enum SyncTaskResult { + Success(Vec), // Success(success_relative_pathes) +} +#[action_gen] +pub async fn track_file_action( + ctx: ActionContext, + arguments: TrackFileActionArguments, +) -> Result { + let relative_pathes = arguments.relative_pathes; + let instance = check_connection_instance(&ctx)?; + + // Auth Member + let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { + Ok(id) => id, + Err(e) => return Ok(TrackFileActionResult::AuthorizeFailed(e.to_string())), + }; + + // Check sheet + let (sheet_name, is_ref_sheet) = + get_current_sheet_name(&ctx, instance, &member_id, true).await?; + + // Can modify Sheet when not in reference sheet or in Host mode + let can_modify_sheet = !is_ref_sheet || is_host_mode; + + if ctx.is_proc_on_local() { + let workspace = try_get_local_workspace(&ctx)?; + let analyzed = AnalyzeResult::analyze_local_status(&workspace).await?; + let latest_file_data = + LatestFileData::read_from(LatestFileData::data_path(&member_id)?).await?; + + if !analyzed.lost.is_empty() || !analyzed.moved.is_empty() { + return Ok(TrackFileActionResult::StructureChangesNotSolved); + } + + let Some(sheet_in_use) = workspace.config().lock().await.sheet_in_use().clone() else { + return Err(TcpTargetError::NotFound("Sheet not found!".to_string())); + }; + + // Read local sheet and member held + let local_sheet = workspace.local_sheet(&member_id, &sheet_in_use).await?; + let cached_sheet = CachedSheet::cached_sheet_data(&sheet_in_use).await?; + let member_held = LatestFileData::read_from(LatestFileData::data_path(&member_id)?).await?; + + let modified = analyzed + .modified + .intersection(&relative_pathes) + .cloned() + .collect::>(); + + // Filter out created files + let created_task = analyzed + .created + .intersection(&relative_pathes) + .cloned() + .collect::>(); + + // Filter out modified files that need to be updated + let mut update_task: Vec = { + let result = modified.iter().filter_map(|p| { + if let Ok(local_data) = local_sheet.mapping_data(p) { + let id = local_data.mapping_vfid(); + let local_ver = local_data.version_when_updated(); + let Some(latest_ver) = latest_file_data.file_version(id) else { + return None; + }; + if let Some(held_member) = member_held.file_holder(id) { + // Check if holder and version match + if held_member == &member_id && local_ver == latest_ver { + return Some(p.clone()); + } + } + }; + None + }); + result.collect() + }; + + let mut skipped_task: Vec = Vec::new(); + + // Filter out files that do not exist locally or have version inconsistencies and need to be synchronized + let mut sync_task: Vec = { + let other: Vec = relative_pathes + .iter() + .filter(|p| !created_task.contains(p) && !update_task.contains(p)) + .cloned() + .collect(); + + let result = other.iter().filter_map(|p| { + // Not exists and not lost, first download + if !workspace.local_path().join(p).exists() && !analyzed.lost.contains(p) { + return Some(p.clone()); + } + + // In cached sheet + if !cached_sheet.mapping().contains_key(p) { + return None; + } + + // In local sheet + let local_sheet_mapping = local_sheet.mapping_data(p).ok()?; + let vfid = local_sheet_mapping.mapping_vfid(); + + if let Some(latest_version) = &latest_file_data.file_version(vfid) { + // Version does not match + if &local_sheet_mapping.version_when_updated() != latest_version { + let modified = modified.contains(p); + if modified && arguments.allow_overwrite_modified { + return Some(p.clone()); + } else if modified && !arguments.allow_overwrite_modified { + // If not allowed to overwrite, join skipped tasks + skipped_task.push(p.clone()); + return None; + } + return Some(p.clone()); + } + } + + // File not held and modified + let holder = latest_file_data.file_holder(vfid); + if (holder.is_none() || &member_id != holder.unwrap()) && modified.contains(p) { + // If allow overwrite modified is true, overwrite the file + if arguments.allow_overwrite_modified { + return Some(p.clone()); + } else { + // If not allowed to overwrite, join skipped tasks + skipped_task.push(p.clone()); + return None; + } + } + + None + }); + result.collect() + }; + + // If the sheet cannot be modified, + // the update_task here should be considered invalid and changed to sync rollback + if !can_modify_sheet { + if arguments.allow_overwrite_modified { + sync_task.append(&mut update_task); + update_task.clear(); + } else { + skipped_task.append(&mut update_task); + update_task.clear(); + } + } + + // Package tasks + let tasks: (Vec, Vec, Vec) = + (created_task, update_task, sync_task); + + // Send to remote + { + let mut mut_instance = instance.lock().await; + mut_instance + .write_large_msgpack(tasks.clone(), 1024u16) + .await?; + // Drop mutex here + } + + // Process create tasks + let mut success_create = Vec::::new(); + if can_modify_sheet { + success_create = match proc_create_tasks_local( + &ctx, + instance.clone(), + &member_id, + &sheet_name, + tasks.0, + arguments.print_infos, + ) + .await + { + Ok(r) => match r { + CreateTaskResult::Success(relative_pathes) => relative_pathes, + _ => { + return Ok(TrackFileActionResult::CreateTaskFailed(r)); + } + }, + Err(e) => return Err(e), + }; + } + + // Process update tasks + let mut success_update = Vec::::new(); + if can_modify_sheet { + success_update = match proc_update_tasks_local( + &ctx, + instance.clone(), + &member_id, + &sheet_name, + tasks.1, + arguments.print_infos, + arguments.file_update_info, + ) + .await + { + Ok(r) => match r { + UpdateTaskResult::Success(relative_pathes) => relative_pathes, + _ => { + return Ok(TrackFileActionResult::UpdateTaskFailed(r)); + } + }, + Err(e) => return Err(e), + }; + } + + // Process sync tasks + let success_sync = match proc_sync_tasks_local( + &ctx, + instance.clone(), + &member_id, + &sheet_name, + tasks.2, + arguments.print_infos, + ) + .await + { + Ok(r) => match r { + SyncTaskResult::Success(relative_pathes) => relative_pathes, + }, + Err(e) => return Err(e), + }; + + if success_create.len() + success_update.len() > 0 { + sign_vault_modified(true).await; + } + + return Ok(TrackFileActionResult::Done { + created: success_create, + updated: success_update, + synced: success_sync, + skipped: skipped_task, + }); + } + + if ctx.is_proc_on_remote() { + // Read tasks + let (created_task, update_task, sync_task): (Vec, Vec, Vec) = { + let mut mut_instance = instance.lock().await; + mut_instance.read_large_msgpack(1024u16).await? + }; + + // Process create tasks + let mut success_create = Vec::::new(); + if can_modify_sheet { + success_create = match proc_create_tasks_remote( + &ctx, + instance.clone(), + &member_id, + &sheet_name, + created_task, + ) + .await + { + Ok(r) => match r { + CreateTaskResult::Success(relative_pathes) => relative_pathes, + _ => { + return Ok(TrackFileActionResult::CreateTaskFailed(r)); + } + }, + Err(e) => return Err(e), + }; + } + + // Process update tasks + let mut success_update = Vec::::new(); + if can_modify_sheet { + success_update = match proc_update_tasks_remote( + &ctx, + instance.clone(), + &member_id, + &sheet_name, + update_task, + arguments.file_update_info, + ) + .await + { + Ok(r) => match r { + UpdateTaskResult::Success(relative_pathes) => relative_pathes, + _ => { + return Ok(TrackFileActionResult::UpdateTaskFailed(r)); + } + }, + Err(e) => return Err(e), + }; + } + + // Process sync tasks + let success_sync = match proc_sync_tasks_remote( + &ctx, + instance.clone(), + &member_id, + &sheet_name, + sync_task, + ) + .await + { + Ok(r) => match r { + SyncTaskResult::Success(relative_pathes) => relative_pathes, + }, + Err(e) => return Err(e), + }; + + return Ok(TrackFileActionResult::Done { + created: success_create, + updated: success_update, + synced: success_sync, + skipped: Vec::new(), // The server doesn't know which files were skipped + }); + } + + Err(TcpTargetError::NoResult("No result.".to_string())) +} + +async fn proc_create_tasks_local( + ctx: &ActionContext, + instance: Arc>, + member_id: &MemberId, + sheet_name: &SheetName, + relative_paths: Vec, + print_infos: bool, +) -> Result { + let workspace = try_get_local_workspace(ctx)?; + let local_output = try_get_local_output(ctx)?; + let mut mut_instance = instance.lock().await; + let mut local_sheet = workspace.local_sheet(member_id, sheet_name).await?; + + if print_infos && relative_paths.len() > 0 { + local_println!(local_output, "Creating {} files...", relative_paths.len()); + } + + // Wait for remote detection of whether the sheet exists + let has_sheet = mut_instance.read_msgpack::().await?; + if !has_sheet { + return Ok(CreateTaskResult::SheetNotFound(sheet_name.clone())); + } + + // Wait for remote detection of whether the file exists + let (hasnt_duplicate, duplicate_path) = mut_instance.read_msgpack::<(bool, PathBuf)>().await?; + if !hasnt_duplicate { + return Ok(CreateTaskResult::CreateFileOnExistPath(duplicate_path)); + } + + let mut success_relative_pathes = Vec::new(); + + // Start sending files + for path in relative_paths { + let full_path = workspace.local_path().join(&path); + + // Send file + if mut_instance.write_file(&full_path).await.is_err() { + continue; + } + + // Read virtual file id and version + let (vfid, version, version_desc) = mut_instance + .read_msgpack::<( + VirtualFileId, + VirtualFileVersion, + VirtualFileVersionDescription, + )>() + .await?; + + // Add mapping to local sheet + let hash = sha1_hash::calc_sha1(&full_path, 2048).await.unwrap().hash; + let time = std::fs::metadata(&full_path)?.modified()?; + local_sheet.add_mapping( + &path.clone(), + LocalMappingMetadata::new( + hash, // hash_when_updated + time, // time_when_updated + std::fs::metadata(&full_path)?.len(), // size_when_updated + version_desc, // version_desc_when_updated + version, // version_when_updated + vfid, // mapping_vfid + time, // last_modifiy_check_itme + false, // last_modifiy_check_result + ), + )?; + + // Print success info + if print_infos { + local_println!(local_output, "+ {}", path.display()); + } + + success_relative_pathes.push(path); + } + + // Write local sheet + local_sheet.write().await?; + + Ok(CreateTaskResult::Success(success_relative_pathes)) +} + +async fn proc_create_tasks_remote( + ctx: &ActionContext, + instance: Arc>, + member_id: &MemberId, + sheet_name: &SheetName, + relative_paths: Vec, +) -> Result { + let vault = try_get_vault(ctx)?; + let mut mut_instance = instance.lock().await; + + // Sheet check + let Ok(mut sheet) = vault.sheet(sheet_name).await else { + // Sheet not found + mut_instance.write_msgpack(false).await?; + return Ok(CreateTaskResult::SheetNotFound(sheet_name.to_string())); + }; + mut_instance.write_msgpack(true).await?; + + // Duplicate create precheck + for path in relative_paths.iter() { + if sheet.mapping().contains_key(path) { + // Duplicate file + mut_instance.write_msgpack((false, path)).await?; + return Ok(CreateTaskResult::CreateFileOnExistPath(path.clone())); + } + } + mut_instance.write_msgpack((true, PathBuf::new())).await?; + + let mut success_relative_pathes = Vec::new(); + + // Start receiving files + for path in relative_paths { + // Read file and create virtual file + let Ok(vfid) = vault + .create_virtual_file_from_connection(&mut mut_instance, member_id) + .await + else { + continue; + }; + + // Record virtual file to sheet + let vf_meta = vault.virtual_file(&vfid)?.read_meta().await?; + sheet + .add_mapping(path.clone(), vfid.clone(), vf_meta.version_latest()) + .await?; + + // Tell client the virtual file id and version + mut_instance + .write_msgpack(( + vfid, + vf_meta.version_latest(), + vf_meta + .version_description(vf_meta.version_latest()) + .unwrap(), + )) + .await?; + + success_relative_pathes.push(path); + } + + sheet.persist().await?; + + Ok(CreateTaskResult::Success(success_relative_pathes)) +} + +async fn proc_update_tasks_local( + ctx: &ActionContext, + instance: Arc>, + member_id: &MemberId, + sheet_name: &SheetName, + relative_paths: Vec, + print_infos: bool, + file_update_info: HashMap, +) -> Result { + let workspace = try_get_local_workspace(ctx)?; + let local_output = try_get_local_output(ctx)?; + let mut mut_instance = instance.lock().await; + let mut local_sheet = workspace.local_sheet(member_id, sheet_name).await?; + + let mut success = Vec::new(); + + if print_infos && relative_paths.len() > 0 { + local_println!(local_output, "Updating {} files...", relative_paths.len()); + } + + for path in relative_paths.iter() { + let Ok(mapping) = local_sheet.mapping_data(path) else { + // Is mapping not found, write empty + mut_instance.write_msgpack("".to_string()).await?; + continue; + }; + // Read and send file version + let Ok(_) = mut_instance + .write_msgpack(mapping.version_when_updated()) + .await + else { + continue; + }; + + // Read verify result + let verify_result: bool = mut_instance.read_msgpack().await?; + if !verify_result { + let reason = mut_instance.read_msgpack::().await?; + return Ok(UpdateTaskResult::VerifyFailed { + path: path.clone(), + reason: reason.clone(), + }); + } + + // Calc hash + let hash_result = match sha1_hash::calc_sha1(workspace.local_path().join(path), 2048).await + { + Ok(r) => r, + Err(_) => { + mut_instance.write_msgpack(false).await?; // Not Ready + continue; + } + }; + + // Get next version + let Some((next_version, description)) = file_update_info.get(path) else { + mut_instance.write_msgpack(false).await?; // Not Ready + continue; + }; + + // Write + mut_instance.write_msgpack(true).await?; // Ready + mut_instance.write_file(path).await?; + + // Read upload result + let upload_result: bool = mut_instance.read_msgpack().await?; + if upload_result { + // Success + let mapping_data_mut = local_sheet.mapping_data_mut(path).unwrap(); + let version = mapping_data_mut.version_when_updated().clone(); + mapping_data_mut.set_hash_when_updated(hash_result.hash); + mapping_data_mut.set_version_when_updated(next_version.clone()); + mapping_data_mut.set_version_desc_when_updated(VirtualFileVersionDescription { + creator: member_id.clone(), + description: description.clone(), + }); + mapping_data_mut.set_last_modifiy_check_result(false); // Mark file not modified + + // Write + local_sheet.write().await?; + + // Push path into success vec + success.push(path.clone()); + + // Print success info + if print_infos { + local_println!( + local_output, + "↑ {} ({} -> {})", + path.display(), + version, + next_version + ); + } + } + } + + Ok(UpdateTaskResult::Success(success)) +} + +async fn proc_update_tasks_remote( + ctx: &ActionContext, + instance: Arc>, + member_id: &MemberId, + sheet_name: &SheetName, + relative_paths: Vec, + file_update_info: HashMap, +) -> Result { + let vault = try_get_vault(ctx)?; + let mut mut_instance = instance.lock().await; + + let mut success = Vec::new(); + + for path in relative_paths.iter() { + // Read version + let Ok(version) = mut_instance.read_msgpack::().await else { + continue; + }; + if version.is_empty() { + continue; + } + + // Verify + let Some((next_version, description)) = file_update_info.get(path) else { + mut_instance.write_msgpack(false).await?; + let reason = VerifyFailReason::UpdateButNoDescription; + mut_instance.write_msgpack(reason.clone()).await?; + return Ok(UpdateTaskResult::VerifyFailed { + path: path.clone(), + reason, + }); // Sheet not found + }; + let Ok(mut sheet) = vault.sheet(sheet_name).await else { + mut_instance.write_msgpack(false).await?; + let reason = VerifyFailReason::SheetNotFound(sheet_name.clone()); + mut_instance.write_msgpack(reason.clone()).await?; + return Ok(UpdateTaskResult::VerifyFailed { + path: path.clone(), + reason, + }); // Sheet not found + }; + let Some(mapping_data) = sheet.mapping_mut().get_mut(path) else { + mut_instance.write_msgpack(false).await?; + let reason = VerifyFailReason::MappingNotFound; + mut_instance.write_msgpack(reason.clone()).await?; + return Ok(UpdateTaskResult::VerifyFailed { + path: path.clone(), + reason, + }); // Mapping not found + }; + let Ok(vf) = vault.virtual_file(&mapping_data.id) else { + mut_instance.write_msgpack(false).await?; + let reason = VerifyFailReason::VirtualFileNotFound(mapping_data.id.clone()); + mut_instance.write_msgpack(reason.clone()).await?; + return Ok(UpdateTaskResult::VerifyFailed { + path: path.clone(), + reason, + }); // Virtual file not found + }; + let Ok(vf_metadata) = vf.read_meta().await else { + mut_instance.write_msgpack(false).await?; + let reason = VerifyFailReason::VirtualFileReadFailed(mapping_data.id.clone()); + mut_instance.write_msgpack(reason.clone()).await?; + return Ok(UpdateTaskResult::VerifyFailed { + path: path.clone(), + reason, + }); // Read virtual file metadata failed + }; + if vf_metadata.versions().contains(next_version) { + mut_instance.write_msgpack(false).await?; + let reason = VerifyFailReason::VersionAlreadyExist(version); + mut_instance.write_msgpack(reason.clone()).await?; + return Ok(UpdateTaskResult::VerifyFailed { + path: path.clone(), + reason, + }); // VersionAlreadyExist + } + if vf_metadata.hold_member() != member_id { + mut_instance.write_msgpack(false).await?; + let reason = VerifyFailReason::NotHeld; + mut_instance.write_msgpack(reason.clone()).await?; + return Ok(UpdateTaskResult::VerifyFailed { + path: path.clone(), + reason, + }); // Member not held it + }; + if vf_metadata.version_latest() != version { + mut_instance.write_msgpack(false).await?; + let reason = + VerifyFailReason::VersionDismatch(version.clone(), vf_metadata.version_latest()); + mut_instance.write_msgpack(reason.clone()).await?; + return Ok(UpdateTaskResult::VerifyFailed { + path: path.clone(), + reason, + }); // Version does not match + }; + mut_instance.write_msgpack(true).await?; // Verified + + // Read if local ready + let ready: bool = mut_instance.read_msgpack().await?; + if !ready { + continue; + } + + // Read and update virtual file + match vault + .update_virtual_file_from_connection( + &mut mut_instance, + member_id, + &mapping_data.id, + next_version, + VirtualFileVersionDescription { + creator: member_id.clone(), + description: description.clone(), + }, + ) + .await + { + Ok(_) => { + // Update version to sheet + mapping_data.version = next_version.clone(); + + // Persist + sheet.persist().await?; + + success.push(path.clone()); + mut_instance.write_msgpack(true).await?; // Success + } + Err(e) => { + mut_instance.write_msgpack(false).await?; // Fail + return Err(e.into()); + } + } + } + + Ok(UpdateTaskResult::Success(success)) +} + +type SyncVersionInfo = Option<( + VirtualFileVersion, + VirtualFileVersionDescription, + VirtualFileId, +)>; + +async fn proc_sync_tasks_local( + ctx: &ActionContext, + instance: Arc>, + member_id: &MemberId, + sheet_name: &SheetName, + relative_paths: Vec, + print_infos: bool, +) -> Result { + let workspace = try_get_local_workspace(ctx)?; + let local_output = try_get_local_output(ctx)?; + let mut mut_instance = instance.lock().await; + let mut success: Vec = Vec::new(); + + if print_infos && relative_paths.len() > 0 { + local_println!(local_output, "Syncing {} files...", relative_paths.len()); + } + + for path in relative_paths { + let Some((version, description, vfid)) = + mut_instance.read_msgpack::().await? + else { + continue; + }; + + // Generate a temp path + let temp_path = workspace + .local_path() + .join(CLIENT_FILE_TEMP_FILE.replace(KEY_TEMP_NAME, &VaultUuid::new_v4().to_string())); + + let copy_to = workspace.local_path().join(&path); + + // Read file + match mut_instance.read_file(&temp_path).await { + Ok(_) => { + if !temp_path.exists() { + continue; + } + } + Err(_) => { + continue; + } + } + + // Calc hash + let new_hash = match calc_sha1(&temp_path, 2048).await { + Ok(hash) => hash, + Err(_) => { + continue; + } + }; + + // Calc size + let new_size = match fs::metadata(&temp_path).await.map(|meta| meta.len()) { + Ok(size) => size, + Err(_) => { + continue; + } + }; + + // Write file + if copy_to.exists() { + if let Err(_) = fs::remove_file(©_to).await { + continue; + } + } else { + // Not exist, create directory + if let Some(path) = copy_to.clone().parent() { + fs::create_dir_all(path).await?; + } + } + if let Err(_) = fs::rename(&temp_path, ©_to).await { + continue; + } + + // Modify local sheet + let mut local_sheet = match workspace.local_sheet(member_id, sheet_name).await { + Ok(sheet) => sheet, + Err(_) => { + continue; + } + }; + + // Get or create mapping + let mapping = match local_sheet.mapping_data_mut(&path) { + Ok(m) => m, + Err(_) => { + // First download + let mut data = LocalMappingMetadata::default(); + data.set_mapping_vfid(vfid); + if let Err(_) = local_sheet.add_mapping(&path, data) { + continue; + } + match local_sheet.mapping_data_mut(&path) { + Ok(m) => m, + Err(_) => { + continue; + } + } + } + }; + + let time = SystemTime::now(); + mapping.set_hash_when_updated(new_hash.hash); + mapping.set_last_modifiy_check_result(false); // Mark not modified + mapping.set_version_when_updated(version); + mapping.set_version_desc_when_updated(description); + mapping.set_size_when_updated(new_size); + mapping.set_time_when_updated(time); + mapping.set_last_modifiy_check_time(time); + if let Err(_) = local_sheet.write().await { + continue; + } + + success.push(path.clone()); + + // Print success info + if print_infos { + local_println!(local_output, "↓ {}", path.display()); + } + } + Ok(SyncTaskResult::Success(success)) +} + +async fn proc_sync_tasks_remote( + ctx: &ActionContext, + instance: Arc>, + _member_id: &MemberId, + sheet_name: &SheetName, + relative_paths: Vec, +) -> Result { + let vault = try_get_vault(ctx)?; + let sheet = vault.sheet(sheet_name).await?; + let mut mut_instance = instance.lock().await; + let mut success: Vec = Vec::new(); + + for path in relative_paths { + // Get mapping + let Some(mapping) = sheet.mapping().get(&path) else { + mut_instance.write_msgpack::(None).await?; // (ready) + continue; + }; + // Get virtual file + let Ok(vf) = vault.virtual_file(&mapping.id) else { + mut_instance.write_msgpack::(None).await?; // (ready) + continue; + }; + // Read metadata and get real path + let vf_meta = &vf.read_meta().await?; + let real_path = vault.virtual_file_real_path(&mapping.id, &vf_meta.version_latest()); + let version = vf_meta.version_latest(); + mut_instance + .write_msgpack::(Some(( + version.clone(), + vf_meta.version_description(version).cloned().unwrap_or( + VirtualFileVersionDescription { + creator: MemberId::default(), + description: "".to_string(), + }, + ), + vf.id(), + ))) + .await?; // (ready) + if mut_instance.write_file(real_path).await.is_err() { + continue; + } else { + success.push(path); + } + } + Ok(SyncTaskResult::Success(success)) +} diff --git a/legacy_actions/src/remote_actions/edit_right_manage.rs b/legacy_actions/src/remote_actions/edit_right_manage.rs new file mode 100644 index 0000000..52305f5 --- /dev/null +++ b/legacy_actions/src/remote_actions/edit_right_manage.rs @@ -0,0 +1 @@ +pub mod change_virtual_file_edit_right; diff --git a/legacy_actions/src/remote_actions/edit_right_manage/change_virtual_file_edit_right.rs b/legacy_actions/src/remote_actions/edit_right_manage/change_virtual_file_edit_right.rs new file mode 100644 index 0000000..1045bce --- /dev/null +++ b/legacy_actions/src/remote_actions/edit_right_manage/change_virtual_file_edit_right.rs @@ -0,0 +1,144 @@ +use std::path::PathBuf; + +use action_system::{action::ActionContext, macros::action_gen}; +use serde::{Deserialize, Serialize}; +use tcp_connection::error::TcpTargetError; +use vcs_data::data::local::modified_status::sign_vault_modified; + +use crate::remote_actions::{ + auth_member, check_connection_instance, get_current_sheet_name, try_get_vault, +}; + +#[derive(Serialize, Deserialize)] +pub enum ChangeVirtualFileEditRightResult { + // Success + Success { + success_hold: Vec, + success_throw: Vec, + }, + + // Fail + AuthorizeFailed(String), + DoNothing, +} + +#[derive(Serialize, Deserialize, PartialEq, Clone)] +pub enum EditRightChangeBehaviour { + Hold, + Throw, +} + +/// The server part only checks: +/// 1. Whether the file exists +/// 2. Whether the file has no holder +/// If both conditions are met, send success information to the local client +/// +/// All version checks are handled locally +#[action_gen] +pub async fn change_virtual_file_edit_right_action( + ctx: ActionContext, + arguments: (Vec<(PathBuf, EditRightChangeBehaviour)>, bool), +) -> Result { + let instance = check_connection_instance(&ctx)?; + let (relative_paths, print_info) = arguments; + + // Auth Member + let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { + Ok(id) => id, + Err(e) => { + return Ok(ChangeVirtualFileEditRightResult::AuthorizeFailed( + e.to_string(), + )); + } + }; + + // Check sheet + let (sheet_name, _is_ref_sheet) = + get_current_sheet_name(&ctx, instance, &member_id, true).await?; + + if ctx.is_proc_on_remote() { + let mut mut_instance = instance.lock().await; + let mut success_hold: Vec = Vec::new(); + let mut success_throw: Vec = Vec::new(); + let vault = try_get_vault(&ctx)?; + for (path, behaviour) in relative_paths { + let Ok(sheet) = vault.sheet(&sheet_name).await else { + continue; + }; + let Some(mapping) = sheet.mapping().get(&path) else { + continue; + }; + let Ok(has_edit_right) = vault + .has_virtual_file_edit_right(&member_id, &mapping.id) + .await + else { + continue; + }; + + // Hold file + if !has_edit_right && behaviour == EditRightChangeBehaviour::Hold { + match vault + .grant_virtual_file_edit_right(&member_id, &mapping.id) + .await + { + Ok(_) => { + success_hold.push(path.clone()); + } + Err(_) => continue, + } + } else + // Throw file + if (has_edit_right || is_host_mode) + && behaviour == EditRightChangeBehaviour::Throw + { + match vault.revoke_virtual_file_edit_right(&mapping.id).await { + Ok(_) => { + success_throw.push(path.clone()); + } + Err(_) => continue, + } + } + } + + // Write success list + mut_instance + .write_large_msgpack::<(Vec, Vec)>( + (success_hold.clone(), success_throw.clone()), + 4096u16, + ) + .await?; + return Ok(ChangeVirtualFileEditRightResult::Success { + success_hold, + success_throw, + }); + } + + if ctx.is_proc_on_local() { + let mut mut_instance = instance.lock().await; + let (success_hold, success_throw) = mut_instance + .read_large_msgpack::<(Vec, Vec)>(4096u16) + .await?; + + // If there are any successful items, mark as modified + if success_hold.len() + success_throw.len() > 0 { + sign_vault_modified(true).await; + } + + // Print info + if print_info { + success_hold + .iter() + .for_each(|s| println!("--> {}", s.display())); + success_throw + .iter() + .for_each(|s| println!("<-- {}", s.display())); + } + + return Ok(ChangeVirtualFileEditRightResult::Success { + success_hold, + success_throw, + }); + } + + Ok(ChangeVirtualFileEditRightResult::DoNothing) +} diff --git a/legacy_actions/src/remote_actions/mapping_manage.rs b/legacy_actions/src/remote_actions/mapping_manage.rs new file mode 100644 index 0000000..624b4ab --- /dev/null +++ b/legacy_actions/src/remote_actions/mapping_manage.rs @@ -0,0 +1,3 @@ +pub mod edit_mapping; +pub mod merge_share_mapping; +pub mod share_mapping; diff --git a/legacy_actions/src/remote_actions/mapping_manage/edit_mapping.rs b/legacy_actions/src/remote_actions/mapping_manage/edit_mapping.rs new file mode 100644 index 0000000..3c39c5d --- /dev/null +++ b/legacy_actions/src/remote_actions/mapping_manage/edit_mapping.rs @@ -0,0 +1,157 @@ +use std::collections::HashMap; + +use action_system::{action::ActionContext, macros::action_gen}; +use serde::{Deserialize, Serialize}; +use tcp_connection::error::TcpTargetError; +use vcs_data::data::local::{ + modified_status::sign_vault_modified, + workspace_analyzer::{FromRelativePathBuf, ToRelativePathBuf}, +}; + +use crate::{ + remote_actions::{ + auth_member, check_connection_instance, get_current_sheet_name, try_get_vault, + }, + write_and_return, +}; + +pub type OperationArgument = (EditMappingOperations, Option); + +#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)] +pub enum EditMappingOperations { + Move, + Erase, +} + +#[derive(Serialize, Deserialize, Default)] +pub enum EditMappingActionResult { + Success, + + // Fail + AuthorizeFailed(String), + EditNotAllowed, + MappingNotFound(FromRelativePathBuf), + InvalidMove(InvalidMoveReason), + + #[default] + Unknown, +} + +#[derive(Serialize, Deserialize)] +pub enum InvalidMoveReason { + MoveOperationButNoTarget(FromRelativePathBuf), + ContainsDuplicateMapping(ToRelativePathBuf), +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct EditMappingActionArguments { + pub operations: HashMap, +} + +/// This Action only modifies Sheet Mapping and +/// does not interfere with the actual location of local files or Local Mapping +#[action_gen] +pub async fn edit_mapping_action( + ctx: ActionContext, + args: EditMappingActionArguments, +) -> Result { + let instance = check_connection_instance(&ctx)?; + + // Auth Member + let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { + Ok(id) => id, + Err(e) => { + return Ok(EditMappingActionResult::AuthorizeFailed(e.to_string())); + } + }; + + // Check sheet + let (sheet_name, is_ref_sheet) = + get_current_sheet_name(&ctx, instance, &member_id, true).await?; + + // Can modify Sheet when not in reference sheet or in Host mode + let can_modify_sheet = !is_ref_sheet || is_host_mode; + + if !can_modify_sheet { + return Ok(EditMappingActionResult::EditNotAllowed); + } + + if ctx.is_proc_on_remote() { + let vault = try_get_vault(&ctx)?; + let mut sheet = vault.sheet(&sheet_name).await?; + + // Precheck + for (from_path, (operation, to_path)) in args.operations.iter() { + // Check mapping exists + if !sheet.mapping().contains_key(from_path) { + write_and_return!( + instance, + EditMappingActionResult::MappingNotFound(from_path.clone()) + ); + } + + // Move check + if operation == &EditMappingOperations::Move { + // Check if target exists + if let Some(to_path) = to_path { + // Check if target is duplicate + if sheet.mapping().contains_key(to_path) { + write_and_return!( + instance, + EditMappingActionResult::InvalidMove( + InvalidMoveReason::ContainsDuplicateMapping(to_path.clone()) + ) + ); + } + } else { + write_and_return!( + instance, + EditMappingActionResult::InvalidMove( + InvalidMoveReason::MoveOperationButNoTarget(from_path.clone()) + ) + ); + } + } + } + + // Process + for (from_path, (operation, to_path)) in args.operations { + match operation { + // During the Precheck phase, it has been ensured that: + // 1. The mapping to be edited for the From path indeed exists + // 2. The location of the To path is indeed empty + // 3. In Move mode, To path can be safely unwrapped + // Therefore, the following unwrap() calls are safe to execute + EditMappingOperations::Move => { + let mapping = sheet.mapping_mut().remove(&from_path).unwrap(); + let to_path = to_path.unwrap(); + sheet + .add_mapping(to_path, mapping.id, mapping.version) + .await?; + } + EditMappingOperations::Erase => { + sheet.mapping_mut().remove(&from_path).unwrap(); + } + } + } + + // Write + sheet.persist().await?; + + write_and_return!(instance, EditMappingActionResult::Success); + } + + if ctx.is_proc_on_local() { + let result = instance + .lock() + .await + .read::() + .await?; + if matches!(result, EditMappingActionResult::Success) { + sign_vault_modified(true).await; + } + return Ok(result); + } + + Ok(EditMappingActionResult::Success) +} diff --git a/legacy_actions/src/remote_actions/mapping_manage/merge_share_mapping.rs b/legacy_actions/src/remote_actions/mapping_manage/merge_share_mapping.rs new file mode 100644 index 0000000..df889a1 --- /dev/null +++ b/legacy_actions/src/remote_actions/mapping_manage/merge_share_mapping.rs @@ -0,0 +1,117 @@ +use std::io::ErrorKind; + +use action_system::{action::ActionContext, macros::action_gen}; +use serde::{Deserialize, Serialize}; +use tcp_connection::error::TcpTargetError; +use vcs_data::data::{ + local::modified_status::sign_vault_modified, + vault::mapping_share::{ShareMergeMode, SheetShareId}, +}; + +use crate::{ + remote_actions::{ + auth_member, check_connection_instance, get_current_sheet_name, try_get_vault, + }, + write_and_return, +}; + +#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)] +pub struct MergeShareMappingArguments { + pub share_id: SheetShareId, + pub share_merge_mode: ShareMergeMode, +} + +#[derive(Serialize, Deserialize, Default)] +pub enum MergeShareMappingActionResult { + Success, + + // Fail + HasConflicts, + AuthorizeFailed(String), + EditNotAllowed, + ShareIdNotFound(SheetShareId), + MergeFails(String), + + #[default] + Unknown, +} + +#[action_gen] +pub async fn merge_share_mapping_action( + ctx: ActionContext, + args: MergeShareMappingArguments, +) -> Result { + let instance = check_connection_instance(&ctx)?; + + // Auth Member + let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { + Ok(id) => id, + Err(e) => { + return Ok(MergeShareMappingActionResult::AuthorizeFailed( + e.to_string(), + )); + } + }; + + // Check sheet + let (sheet_name, is_ref_sheet) = + get_current_sheet_name(&ctx, instance, &member_id, true).await?; + + // Can modify Sheet when not in reference sheet or in Host mode + let can_modify_sheet = !is_ref_sheet || is_host_mode; + + if !can_modify_sheet { + return Ok(MergeShareMappingActionResult::EditNotAllowed); + } + + if ctx.is_proc_on_remote() { + let vault = try_get_vault(&ctx)?; + let share_id = args.share_id; + + // Get the share and sheet + let (sheet, share) = if vault.share_file_path(&sheet_name, &share_id).exists() { + let sheet = vault.sheet(&sheet_name).await?; + let share = sheet.get_share(&share_id).await?; + (sheet, share) + } else { + // Share does not exist + write_and_return!( + instance, + MergeShareMappingActionResult::ShareIdNotFound(share_id.clone()) + ); + }; + + // Perform the merge + match sheet.merge_share(share, args.share_merge_mode).await { + Ok(_) => write_and_return!(instance, MergeShareMappingActionResult::Success), + Err(e) => match e.kind() { + ErrorKind::AlreadyExists => { + write_and_return!(instance, MergeShareMappingActionResult::HasConflicts); + } + _ => { + write_and_return!( + instance, + MergeShareMappingActionResult::MergeFails(e.to_string()) + ); + } + }, + } + } + + if ctx.is_proc_on_local() { + let result = instance + .lock() + .await + .read::() + .await?; + match result { + MergeShareMappingActionResult::Success => { + sign_vault_modified(true).await; + } + _ => {} + } + return Ok(result); + } + + Ok(MergeShareMappingActionResult::Success) +} diff --git a/legacy_actions/src/remote_actions/mapping_manage/share_mapping.rs b/legacy_actions/src/remote_actions/mapping_manage/share_mapping.rs new file mode 100644 index 0000000..5c77e53 --- /dev/null +++ b/legacy_actions/src/remote_actions/mapping_manage/share_mapping.rs @@ -0,0 +1,135 @@ +use action_system::{action::ActionContext, macros::action_gen}; +use serde::{Deserialize, Serialize}; +use tcp_connection::error::TcpTargetError; +use vcs_data::{ + constants::VAULT_HOST_NAME, + data::{local::workspace_analyzer::FromRelativePathBuf, sheet::SheetName}, +}; + +use crate::{ + remote_actions::{ + auth_member, check_connection_instance, get_current_sheet_name, try_get_vault, + }, + write_and_return, +}; + +#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)] +pub struct ShareMappingArguments { + pub mappings: Vec, + pub description: String, + // None = current sheet, + // Some(sheet_name) = other ref(public) sheet + pub from_sheet: Option, + pub to_sheet: SheetName, +} + +#[derive(Serialize, Deserialize, Default)] +pub enum ShareMappingActionResult { + Success, + + // Fail + AuthorizeFailed(String), + TargetSheetNotFound(SheetName), + TargetIsSelf, + MappingNotFound(FromRelativePathBuf), + + #[default] + Unknown, +} + +#[action_gen] +pub async fn share_mapping_action( + ctx: ActionContext, + args: ShareMappingArguments, +) -> Result { + let instance = check_connection_instance(&ctx)?; + + // Auth Member + let (member_id, _is_host_mode) = match auth_member(&ctx, instance).await { + Ok(id) => id, + Err(e) => { + return Ok(ShareMappingActionResult::AuthorizeFailed(e.to_string())); + } + }; + + // Check sheet + let sheet_name = args.from_sheet.unwrap_or( + get_current_sheet_name(&ctx, instance, &member_id, true) + .await? + .0, + ); + + if ctx.is_proc_on_remote() { + let vault = try_get_vault(&ctx)?; + let sheet = vault.sheet(&sheet_name).await?; + + // Tip: Because sheet_name may specify a sheet that does not belong to the user, + // a secondary verification is required. + + // Check if the sheet holder is Some and matches the member_id or is the host + let Some(holder) = sheet.holder() else { + // If there's no holder, the sheet cannot be shared from + write_and_return!( + instance, + ShareMappingActionResult::AuthorizeFailed("Sheet has no holder".to_string()) + ); + }; + + // Verify the holder is either the current member or the host + if holder != &member_id && holder != VAULT_HOST_NAME { + write_and_return!( + instance, + ShareMappingActionResult::AuthorizeFailed( + "Not sheet holder or ref sheet".to_string() + ) + ); + } + + let to_sheet_name = args.to_sheet; + + // Verify target sheet exists + if !vault.sheet_names()?.contains(&to_sheet_name) { + // Does not exist + write_and_return!( + instance, + ShareMappingActionResult::TargetSheetNotFound(to_sheet_name.clone()) + ); + } + + // Verify sheet is not self + if sheet_name == to_sheet_name { + // Is self + write_and_return!(instance, ShareMappingActionResult::TargetIsSelf); + } + + // Verify all mappings are correct + for mapping in args.mappings.iter() { + if !sheet.mapping().contains_key(mapping) { + // If any mapping is invalid, indicate failure + write_and_return!( + instance, + ShareMappingActionResult::MappingNotFound(mapping.clone()) + ); + } + } + + // Execute sharing logic + sheet + .share_mappings(&to_sheet_name, args.mappings, &member_id, args.description) + .await?; + + // Sharing successful + write_and_return!(instance, ShareMappingActionResult::Success); + } + + if ctx.is_proc_on_local() { + let result = instance + .lock() + .await + .read::() + .await?; + return Ok(result); + } + + Ok(ShareMappingActionResult::Success) +} diff --git a/legacy_actions/src/remote_actions/sheet_manage.rs b/legacy_actions/src/remote_actions/sheet_manage.rs new file mode 100644 index 0000000..a3a91a2 --- /dev/null +++ b/legacy_actions/src/remote_actions/sheet_manage.rs @@ -0,0 +1,2 @@ +pub mod drop_sheet; +pub mod make_sheet; diff --git a/legacy_actions/src/remote_actions/sheet_manage/drop_sheet.rs b/legacy_actions/src/remote_actions/sheet_manage/drop_sheet.rs new file mode 100644 index 0000000..e21f3dd --- /dev/null +++ b/legacy_actions/src/remote_actions/sheet_manage/drop_sheet.rs @@ -0,0 +1,123 @@ +use std::io::ErrorKind; + +use action_system::{action::ActionContext, macros::action_gen}; +use serde::{Deserialize, Serialize}; +use tcp_connection::error::TcpTargetError; +use vcs_data::data::{local::modified_status::sign_vault_modified, sheet::SheetName}; + +use crate::{ + remote_actions::{ + auth_member, check_connection_instance, try_get_local_workspace, try_get_vault, + }, + write_and_return, +}; + +#[derive(Default, Serialize, Deserialize)] +pub enum DropSheetActionResult { + Success, + + // Fail + SheetInUse, + AuthorizeFailed(String), + SheetNotExists, + SheetDropFailed(String), + NoHolder, + NotOwner, + + #[default] + Unknown, +} + +#[action_gen] +pub async fn drop_sheet_action( + ctx: ActionContext, + sheet_name: SheetName, +) -> Result { + let instance = check_connection_instance(&ctx)?; + + // Auth Member + let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { + Ok(id) => id, + Err(e) => { + return Ok(DropSheetActionResult::AuthorizeFailed(e.to_string())); + } + }; + + // Check sheet in use on local + if ctx.is_proc_on_local() { + let local_workspace = try_get_local_workspace(&ctx)?; + if let Some(sheet) = local_workspace.config().lock().await.sheet_in_use() { + if sheet == &sheet_name { + instance.lock().await.write(false).await?; + return Ok(DropSheetActionResult::SheetInUse); + } + instance.lock().await.write(true).await?; + } else { + instance.lock().await.write(true).await?; + } + } + + if ctx.is_proc_on_remote() { + // Check if client sheet is in use + let sheet_in_use = instance.lock().await.read::().await?; + if !sheet_in_use { + return Ok(DropSheetActionResult::SheetInUse); + } + + let vault = try_get_vault(&ctx)?; + + // Check if the sheet exists + let mut sheet = match vault.sheet(&sheet_name).await { + Ok(sheet) => sheet, + Err(e) => { + if e.kind() == ErrorKind::NotFound { + write_and_return!(instance, DropSheetActionResult::SheetNotExists); + } else { + write_and_return!( + instance, + DropSheetActionResult::SheetDropFailed(e.to_string()) + ); + } + } + }; + + // Get the sheet's holder + let Some(holder) = sheet.holder() else { + write_and_return!(instance, DropSheetActionResult::NoHolder); + }; + + // Verify that the sheet holder is either the current user or the host + // All sheets belong to the host + if holder != &member_id && !is_host_mode { + write_and_return!(instance, DropSheetActionResult::NotOwner); + } + + // Drop the sheet + sheet.forget_holder(); + match sheet.persist().await { + Ok(_) => { + write_and_return!(instance, DropSheetActionResult::Success); + } + Err(e) => { + write_and_return!( + instance, + DropSheetActionResult::SheetDropFailed(e.to_string()) + ); + } + } + } + + if ctx.is_proc_on_local() { + let result = instance + .lock() + .await + .read::() + .await?; + if matches!(result, DropSheetActionResult::Success) { + sign_vault_modified(true).await; + } + return Ok(result); + } + + Err(TcpTargetError::NoResult("No result.".to_string())) +} diff --git a/legacy_actions/src/remote_actions/sheet_manage/make_sheet.rs b/legacy_actions/src/remote_actions/sheet_manage/make_sheet.rs new file mode 100644 index 0000000..a323413 --- /dev/null +++ b/legacy_actions/src/remote_actions/sheet_manage/make_sheet.rs @@ -0,0 +1,98 @@ +use action_system::{action::ActionContext, macros::action_gen}; +use serde::{Deserialize, Serialize}; +use tcp_connection::error::TcpTargetError; +use vcs_data::{ + constants::VAULT_HOST_NAME, + data::{local::modified_status::sign_vault_modified, sheet::SheetName}, +}; + +use crate::{ + remote_actions::{auth_member, check_connection_instance, try_get_vault}, + write_and_return, +}; + +#[derive(Default, Serialize, Deserialize)] +pub enum MakeSheetActionResult { + Success, + SuccessRestore, + + // Fail + AuthorizeFailed(String), + SheetAlreadyExists, + SheetCreationFailed(String), + + #[default] + Unknown, +} + +/// Build a sheet with context +#[action_gen] +pub async fn make_sheet_action( + ctx: ActionContext, + sheet_name: SheetName, +) -> Result { + let instance = check_connection_instance(&ctx)?; + + // Auth Member + let (member_id, is_host_mode) = match auth_member(&ctx, instance).await { + Ok(id) => id, + Err(e) => return Ok(MakeSheetActionResult::AuthorizeFailed(e.to_string())), + }; + + if ctx.is_proc_on_remote() { + let vault = try_get_vault(&ctx)?; + let holder = if is_host_mode { + VAULT_HOST_NAME.to_string() + } else { + member_id + }; + + // Check if the sheet already exists + if let Ok(mut sheet) = vault.sheet(&sheet_name).await { + // If the sheet has no holder, assign it to the current member (restore operation) + if sheet.holder().is_none() { + sheet.set_holder(holder.clone()); + match sheet.persist().await { + Ok(_) => { + write_and_return!(instance, MakeSheetActionResult::SuccessRestore); + } + Err(e) => { + write_and_return!( + instance, + MakeSheetActionResult::SheetCreationFailed(e.to_string()) + ); + } + } + } else { + write_and_return!(instance, MakeSheetActionResult::SheetAlreadyExists); + } + } else { + // Create the sheet + match vault.create_sheet(&sheet_name, &holder).await { + Ok(_) => { + write_and_return!(instance, MakeSheetActionResult::Success); + } + Err(e) => { + write_and_return!( + instance, + MakeSheetActionResult::SheetCreationFailed(e.to_string()) + ); + } + } + } + } + + if ctx.is_proc_on_local() { + let result = instance + .lock() + .await + .read::() + .await?; + if matches!(result, MakeSheetActionResult::Success) { + sign_vault_modified(true).await; + } + return Ok(result); + } + + Err(TcpTargetError::NoResult("No result.".to_string())) +} diff --git a/legacy_actions/src/remote_actions/workspace_manage.rs b/legacy_actions/src/remote_actions/workspace_manage.rs new file mode 100644 index 0000000..15f70e8 --- /dev/null +++ b/legacy_actions/src/remote_actions/workspace_manage.rs @@ -0,0 +1,2 @@ +pub mod set_upstream_vault; +pub mod update_to_latest_info; diff --git a/legacy_actions/src/remote_actions/workspace_manage/set_upstream_vault.rs b/legacy_actions/src/remote_actions/workspace_manage/set_upstream_vault.rs new file mode 100644 index 0000000..ba45214 --- /dev/null +++ b/legacy_actions/src/remote_actions/workspace_manage/set_upstream_vault.rs @@ -0,0 +1,101 @@ +use std::net::SocketAddr; + +use action_system::{action::ActionContext, macros::action_gen}; +use cfg_file::config::ConfigFile; +use log::info; +use serde::{Deserialize, Serialize}; +use tcp_connection::error::TcpTargetError; +use vcs_data::data::{local::workspace_config::LocalConfig, vault::vault_config::VaultUuid}; + +use crate::remote_actions::{ + auth_member, check_connection_instance, try_get_local_workspace, try_get_vault, +}; + +#[derive(Serialize, Deserialize)] +pub enum SetUpstreamVaultActionResult { + // Success + DirectedAndStained, + Redirected, + + // Fail + AlreadyStained, + AuthorizeFailed(String), + RedirectFailed(String), + SameUpstream, + + Done, +} + +#[action_gen] +pub async fn set_upstream_vault_action( + ctx: ActionContext, + upstream: SocketAddr, +) -> Result { + let instance = check_connection_instance(&ctx)?; + + // Auth Member + if let Err(e) = auth_member(&ctx, instance).await { + return Ok(SetUpstreamVaultActionResult::AuthorizeFailed(e.to_string())); + } + + // Direct + if ctx.is_proc_on_remote() { + let vault = try_get_vault(&ctx)?; + instance + .lock() + .await + .write(*vault.config().vault_uuid()) + .await?; + return Ok(SetUpstreamVaultActionResult::Done); + } + + if ctx.is_proc_on_local() { + info!("Authorize successful. directing to upstream vault."); + + // Read the vault UUID from the instance + let vault_uuid = instance.lock().await.read::().await?; + + let local_workspace = try_get_local_workspace(&ctx)?; + let local_config = local_workspace.config(); + + let mut mut_local_config = local_config.lock().await; + if !mut_local_config.stained() { + // Stain the local workspace + mut_local_config.stain(vault_uuid); + + // Set the upstream address + mut_local_config.set_vault_addr(upstream); + + // Store the updated config + LocalConfig::write(&mut_local_config).await?; + + info!("Workspace stained!"); + return Ok(SetUpstreamVaultActionResult::DirectedAndStained); + } else { + // Local workspace is already stained, redirecting + let Some(stained_uuid) = mut_local_config.stained_uuid() else { + return Ok(SetUpstreamVaultActionResult::RedirectFailed( + "Stained uuid not found".to_string(), + )); + }; + let local_upstream = mut_local_config.upstream_addr(); + + // Address changed, but same UUID. + if vault_uuid == stained_uuid { + if local_upstream != upstream { + // Set the upstream address + mut_local_config.set_vault_addr(upstream); + + // Store the updated config + LocalConfig::write(&mut_local_config).await?; + return Ok(SetUpstreamVaultActionResult::Redirected); + } else { + return Ok(SetUpstreamVaultActionResult::SameUpstream); + } + } + return Ok(SetUpstreamVaultActionResult::AlreadyStained); + } + } + + Err(TcpTargetError::NoResult("No result.".to_string())) +} diff --git a/legacy_actions/src/remote_actions/workspace_manage/update_to_latest_info.rs b/legacy_actions/src/remote_actions/workspace_manage/update_to_latest_info.rs new file mode 100644 index 0000000..cd17c32 --- /dev/null +++ b/legacy_actions/src/remote_actions/workspace_manage/update_to_latest_info.rs @@ -0,0 +1,433 @@ +use std::{ + collections::{HashMap, HashSet}, + io::ErrorKind, + path::PathBuf, + time::SystemTime, +}; + +use action_system::{action::ActionContext, macros::action_gen}; +use cfg_file::config::ConfigFile; +use log::info; +use serde::{Deserialize, Serialize}; +use tcp_connection::error::TcpTargetError; +use vcs_data::{ + constants::{ + CLIENT_PATH_CACHED_SHEET, CLIENT_PATH_LOCAL_SHEET, REF_SHEET_NAME, + SERVER_SUFFIX_SHEET_SHARE_FILE, VAULT_HOST_NAME, + }, + data::{ + local::{ + cached_sheet::CachedSheet, + latest_file_data::LatestFileData, + latest_info::{LatestInfo, SheetInfo}, + modified_status::sign_vault_modified, + }, + member::MemberId, + sheet::{SheetData, SheetName, SheetPathBuf}, + vault::{ + mapping_share::{Share, SheetShareId}, + virtual_file::{VirtualFileId, VirtualFileVersion, VirtualFileVersionDescription}, + }, + }, +}; + +use crate::remote_actions::{ + auth_member, check_connection_instance, try_get_local_workspace, try_get_vault, +}; + +#[derive(Serialize, Deserialize)] +pub enum UpdateToLatestInfoResult { + Success, + + // Fail + AuthorizeFailed(String), + SyncCachedSheetFail(SyncCachedSheetFailReason), +} + +#[derive(Serialize, Deserialize)] +pub enum SyncCachedSheetFailReason { + PathAlreadyExist(PathBuf), +} + +#[action_gen] +pub async fn update_to_latest_info_action( + ctx: ActionContext, + _unused: (), +) -> Result { + let instance = check_connection_instance(&ctx)?; + + let (member_id, _is_host_mode) = match auth_member(&ctx, instance).await { + Ok(id) => id, + Err(e) => return Ok(UpdateToLatestInfoResult::AuthorizeFailed(e.to_string())), + }; + + info!("Sending latest info to {}", member_id); + + // Sync Latest Info + { + if ctx.is_proc_on_remote() { + let vault = try_get_vault(&ctx)?; + + // Build latest info + let mut latest_info = LatestInfo::default(); + + // Sheet & Share + let mut shares_in_my_sheets: HashMap> = + HashMap::new(); + let mut member_owned = Vec::new(); + let mut member_visible = Vec::new(); + let mut ref_sheets = HashSet::new(); + + for sheet in vault.sheets().await? { + // Build share parts + if let Some(holder) = sheet.holder() { + if holder == &member_id || holder == VAULT_HOST_NAME { + let mut sheet_shares: HashMap = HashMap::new(); + for share in sheet.get_shares().await? { + // Get SharePath + let Some(share_path) = share.path.clone() else { + continue; + }; + // Get ShareId from SharePath + let Some(share_id) = share_path.file_name() else { + continue; + }; + let share_id = share_id.display().to_string(); + let share_id_trimed = + share_id.trim_end_matches(SERVER_SUFFIX_SHEET_SHARE_FILE); + sheet_shares.insert(share_id_trimed.to_string(), share); + } + shares_in_my_sheets.insert(sheet.name().clone(), sheet_shares); + } + } + + // Build sheet parts + let holder_is_host = + sheet.holder().unwrap_or(&String::default()) == &VAULT_HOST_NAME; + if sheet.holder().is_some() + && (sheet.holder().unwrap() == &member_id || holder_is_host) + { + member_owned.push(sheet.name().clone()); + if holder_is_host { + ref_sheets.insert(sheet.name().clone()); + } + } else { + member_visible.push(SheetInfo { + sheet_name: sheet.name().clone(), + holder_name: sheet.holder().cloned(), + }); + } + } + + // Record Share & Sheet + latest_info.visible_sheets = member_owned; + latest_info.invisible_sheets = member_visible; + latest_info.shares_in_my_sheets = shares_in_my_sheets; + + // RefSheet + let ref_sheet_data = vault.sheet(&REF_SHEET_NAME.to_string()).await?.to_data(); + latest_info.ref_sheet_content = ref_sheet_data.clone(); + latest_info.ref_sheet_vfs_mapping = ref_sheet_data + .mapping() + .into_iter() + .map(|(path, file)| (file.id.clone(), path.clone())) + .collect::>(); + latest_info.reference_sheets = ref_sheets; + + // Members + let members = vault.members().await?; + latest_info.vault_members = members; + + // Send + instance + .lock() + .await + .write_large_msgpack(latest_info, 512_u16) + .await?; + } + + if ctx.is_proc_on_local() { + let workspace = try_get_local_workspace(&ctx)?; + let mut latest_info = instance + .lock() + .await + .read_large_msgpack::(512_u16) + .await?; + latest_info.update_instant = Some(SystemTime::now()); + LatestInfo::write_to( + &latest_info, + LatestInfo::latest_info_path(workspace.local_path(), &member_id), + ) + .await?; + } + } + + info!("Update sheets to {}", member_id); + + // Sync Remote Sheets + { + if ctx.is_proc_on_local() { + let workspace = try_get_local_workspace(&ctx)?; + let Ok(latest_info) = LatestInfo::read_from(LatestInfo::latest_info_path( + workspace.local_path(), + &member_id, + )) + .await + else { + return Err(TcpTargetError::Io("Read latest info failed".to_string())); + }; + + // Collect all local versions + let mut local_versions = vec![]; + for request_sheet in latest_info.visible_sheets { + let Ok(data) = CachedSheet::cached_sheet_data(&request_sheet).await else { + // For newly created sheets, the version is 0. + // Send -1 to distinguish from 0, ensuring the upstream will definitely send the sheet information + local_versions.push((request_sheet, -1)); + continue; + }; + local_versions.push((request_sheet, data.write_count())); + } + + // Send the version list + let len = local_versions.len(); + instance.lock().await.write_msgpack(local_versions).await?; + + if len < 1 { + // Don't return here, continue to next section + // But we need to consume the false marker from the server + if ctx.is_proc_on_local() { + let mut mut_instance = instance.lock().await; + let _: bool = mut_instance.read_msgpack().await?; + } + } else { + // Receive data + if ctx.is_proc_on_local() { + let mut mut_instance = instance.lock().await; + loop { + let in_coming: bool = mut_instance.read_msgpack().await?; + if in_coming { + let (sheet_name, data): (SheetName, SheetData) = + mut_instance.read_large_msgpack(1024u16).await?; + + let Some(path) = CachedSheet::cached_sheet_path(sheet_name) else { + return Err(TcpTargetError::NotFound( + "Workspace not found".to_string(), + )); + }; + + SheetData::write_to(&data, path).await?; + } else { + break; + } + } + } + } + } + if ctx.is_proc_on_remote() { + let vault = try_get_vault(&ctx)?; + let mut mut_instance = instance.lock().await; + + let local_versions = mut_instance.read_msgpack::>().await?; + + for (sheet_name, version) in local_versions.iter() { + let sheet = vault.sheet(sheet_name).await?; + if let Some(holder) = sheet.holder() + && (holder == &member_id || holder == VAULT_HOST_NAME) + && &sheet.write_count() != version + { + mut_instance.write_msgpack(true).await?; + mut_instance + .write_large_msgpack((sheet_name, sheet.to_data()), 1024u16) + .await?; + } + } + mut_instance.write_msgpack(false).await?; + } + } + + info!("Fetch held status to {}", member_id); + + // Sync Held Info + { + if ctx.is_proc_on_local() { + let workspace = try_get_local_workspace(&ctx)?; + + let Ok(latest_info) = LatestInfo::read_from(LatestInfo::latest_info_path( + workspace.local_path(), + &member_id, + )) + .await + else { + return Err(TcpTargetError::Io("Read latest info failed".to_string())); + }; + + // Collect files that need to know the holder + let mut holder_wants_know = Vec::new(); + for sheet_name in &latest_info.visible_sheets { + if let Ok(sheet_data) = CachedSheet::cached_sheet_data(sheet_name).await { + holder_wants_know + .extend(sheet_data.mapping().values().map(|value| value.id.clone())); + } + } + + // Send request + let mut mut_instance = instance.lock().await; + mut_instance + .write_large_msgpack(&holder_wants_know, 1024u16) + .await?; + + // Receive information and write to local + let result: HashMap< + VirtualFileId, + ( + Option, + VirtualFileVersion, + Vec<(VirtualFileVersion, VirtualFileVersionDescription)>, + ), + > = mut_instance.read_large_msgpack(1024u16).await?; + + // Read configuration file + let path = LatestFileData::data_path(&member_id)?; + let mut latest_file_data: LatestFileData = + LatestFileData::read_from(&path).await.unwrap_or_default(); + + // Write the received information + latest_file_data.update_info(result); + + // Write + LatestFileData::write_to(&latest_file_data, &path).await?; + } + + if ctx.is_proc_on_remote() { + let vault = try_get_vault(&ctx)?; + let mut mut_instance = instance.lock().await; + + // Read the request + let holder_wants_know: Vec = + mut_instance.read_large_msgpack(1024u16).await?; + + // Organize the information + let mut result: HashMap< + VirtualFileId, + ( + Option, + VirtualFileVersion, + Vec<(VirtualFileVersion, VirtualFileVersionDescription)>, + ), + > = HashMap::new(); + for id in holder_wants_know { + let Ok(meta) = vault.virtual_file_meta(&id).await else { + continue; + }; + let holder = if meta.hold_member().is_empty() { + None + } else { + Some(meta.hold_member().clone()) + }; + let latest_version = meta.version_latest(); + + let all_versions = meta.versions(); + let all_descriptions = meta.version_descriptions(); + let histories = all_versions + .iter() + .filter_map(|v| { + let Some(desc) = all_descriptions.get(v) else { + return None; + }; + Some((v.clone(), desc.clone())) + }) + .collect::>(); + + result.insert(id, (holder, latest_version, histories)); + } + + // Send information + mut_instance.write_large_msgpack(&result, 1024u16).await?; + } + } + + // Sync cached sheet to local sheet + if ctx.is_proc_on_local() { + let workspace = try_get_local_workspace(&ctx)?; + let cached_sheet_path = workspace.local_path().join(CLIENT_PATH_CACHED_SHEET); + let local_sheet_path = workspace.local_path().join(CLIENT_PATH_LOCAL_SHEET); + if !local_sheet_path.exists() || !cached_sheet_path.exists() { + // No need to sync + if ctx.is_proc_on_local() { + sign_vault_modified(false).await; + } + return Ok(UpdateToLatestInfoResult::Success); + } + + let cached_sheet_paths = + extract_sheet_names_from_paths(CachedSheet::cached_sheet_paths().await?)?; + + // Match cached sheets and local sheets, and sync content + for (cached_sheet_name, _cached_sheet_path) in cached_sheet_paths { + // Read cached sheet and local sheet + let cached_sheet = CachedSheet::cached_sheet_data(&cached_sheet_name).await?; + let Ok(mut local_sheet) = workspace.local_sheet(&member_id, &cached_sheet_name).await + else { + continue; + }; + + // Read cached id mapping + let Some(cached_sheet_id_mapping) = cached_sheet.id_mapping() else { + continue; + }; + + for (cached_item_id, cached_item_path) in cached_sheet_id_mapping.iter() { + let path_by_id = { local_sheet.path_by_id(cached_item_id).cloned() }; + + // Get local path + let Some(local_path) = path_by_id else { + continue; + }; + + if &local_path == cached_item_path { + continue; + } + + // If path not match, try to move + let move_result = local_sheet.move_mapping(&local_path, cached_item_path); + if let Err(e) = move_result { + match e.kind() { + ErrorKind::AlreadyExists => { + return Ok(UpdateToLatestInfoResult::SyncCachedSheetFail( + SyncCachedSheetFailReason::PathAlreadyExist( + cached_item_path.clone(), + ), + )); + } + _ => return Err(e.into()), + } + } + local_sheet.write().await?; + } + } + } + + if ctx.is_proc_on_local() { + sign_vault_modified(false).await; + } + Ok(UpdateToLatestInfoResult::Success) +} + +/// Extract sheet names from file paths +fn extract_sheet_names_from_paths( + paths: Vec, +) -> Result, std::io::Error> { + let mut result = HashMap::new(); + for p in paths { + let sheet_name = p + .file_stem() + .and_then(|s| s.to_str()) + .map(|s| s.to_string()) + .ok_or_else(|| { + std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid file name") + })?; + result.insert(sheet_name, p); + } + Ok(result) +} diff --git a/legacy_data/Cargo.toml b/legacy_data/Cargo.toml new file mode 100644 index 0000000..7506814 --- /dev/null +++ b/legacy_data/Cargo.toml @@ -0,0 +1,40 @@ +[package] +name = "vcs_data" +edition = "2024" +version.workspace = true + +[dependencies] + +# Utils +cfg_file = { path = "../utils/cfg_file", features = ["default"] } +data_struct = { path = "../utils/data_struct" } +sha1_hash = { path = "../utils/sha1_hash" } +tcp_connection = { path = "../utils/tcp_connection" } +string_proc = { path = "../utils/string_proc" } + +# Core +action_system = { path = "../systems/action" } +vcs_docs = { path = "../docs" } + +# Random +rand = "0.9.2" + +# Identity +uuid = { version = "1.18.1", features = ["v4", "serde"] } +whoami = "1.6.1" + +# Serialization +serde = { version = "1.0.228", features = ["derive"] } + +# Async & Networking +tokio = { version = "1.48.0", features = ["full"] } + +# Filesystem +dirs = "6.0.0" +walkdir = "2.5.0" + +# Time +chrono = "0.4.42" + +# Windows API +winapi = { version = "0.3.9", features = ["fileapi", "winbase", "winnt"] } diff --git a/legacy_data/src/constants.rs b/legacy_data/src/constants.rs new file mode 100644 index 0000000..bc3343f --- /dev/null +++ b/legacy_data/src/constants.rs @@ -0,0 +1,140 @@ +// ------------------------------------------------------------------------------------- + +// Project +pub const PATH_TEMP: &str = "./.temp/"; + +// Default Port +pub const PORT: u16 = 25331; + +// Vault Host Name +pub const VAULT_HOST_NAME: &str = "host"; + +// Default Virtual File Version and Direction +pub const DEFAULT_VF_VERSION: &str = "0.1.0"; +pub const DEFAULT_VF_DESCRIPTION: &str = "_"; + +// ------------------------------------------------------------------------------------- + +// Prefix +pub const VF_PREFIX: &str = "vf-"; + +// ------------------------------------------------------------------------------------- + +// Suffix +pub const SERVER_SUFFIX_SHEET_FILE: &str = ".st"; +pub const SERVER_SUFFIX_SHEET_FILE_NO_DOT: &str = "st"; + +pub const SERVER_SUFFIX_SHEET_SHARE_FILE: &str = ".sre"; +pub const SERVER_SUFFIX_SHEET_SHARE_FILE_NO_DOT: &str = "sre"; + +pub const SERVER_SUFFIX_MEMBER_INFO: &str = ".json"; +pub const SERVER_SUFFIX_MEMBER_INFO_NO_DOT: &str = "json"; + +pub const SERVER_SUFFIX_VF_META: &str = ".vf"; +pub const SERVER_SUFFIX_VF_META_NO_DOT: &str = "vf"; + +pub const CLIENT_SUFFIX_LATEST_INFO: &str = ".up"; +pub const CLIENT_SUFFIX_LATEST_INFO_NO_DOT: &str = "up"; + +pub const CLIENT_SUFFIX_LATEST_DATA: &str = ".upf"; +pub const CLIENT_SUFFIX_LATEST_DATA_NO_DOT: &str = "upf"; + +pub const CLIENT_SUFFIX_LOCAL_SHEET_FILE: &str = ".lst"; +pub const CLIENT_SUFFIX_LOCAL_SHEET_FILE_NO_DOT: &str = "lst"; + +pub const CLIENT_SUFFIX_CACHED_SHEET_FILE: &str = ".st"; +pub const CLIENT_SUFFIX_CACHED_SHEET_FILE_NO_DOT: &str = "st"; + +// ------------------------------------------------------------------------------------- + +// Keys +pub const KEY_SHEET_NAME: &str = "{sheet_name}"; +pub const KEY_MEMBER_ID: &str = "{member_id}"; +pub const KEY_TEMP_NAME: &str = "{temp_name}"; +pub const KEY_VF_INDEX: &str = "{vf_index}"; +pub const KEY_VF_ID: &str = "{vf_id}"; +pub const KEY_VF_VERSION: &str = "{vf_version}"; +pub const KEY_ACCOUNT: &str = "{account}"; +pub const KEY_SELF_ID: &str = "{self_id}"; +pub const KEY_SHARE_ID: &str = "{share_id}"; + +// ------------------------------------------------------------------------------------- + +// Server +// Server - Vault (Main) +pub const SERVER_FILE_VAULT: &str = "./vault.toml"; + +// Server - Sheets +pub const REF_SHEET_NAME: &str = "ref"; +pub const SERVER_PATH_SHEETS: &str = "./sheets/"; +pub const SERVER_PATH_SHARES: &str = "./sheets/shares/{sheet_name}/"; +pub const SERVER_FILE_SHEET: &str = "./sheets/{sheet_name}.st"; +pub const SERVER_FILE_SHEET_SHARE: &str = "./sheets/shares/{sheet_name}/{share_id}.sre"; + +// Server - Members +pub const SERVER_PATH_MEMBERS: &str = "./members/"; +pub const SERVER_PATH_MEMBER_PUB: &str = "./key/"; +pub const SERVER_FILE_MEMBER_INFO: &str = "./members/{member_id}.json"; +pub const SERVER_FILE_MEMBER_PUB: &str = "./key/{member_id}.pem"; + +// Server - Virtual File Storage +pub const SERVER_PATH_VF_TEMP: &str = "./.temp/{temp_name}"; +pub const SERVER_PATH_VF_ROOT: &str = "./storage/"; +pub const SERVER_PATH_VF_STORAGE: &str = "./storage/{vf_index}/{vf_id}/"; +pub const SERVER_FILE_VF_VERSION_INSTANCE: &str = "./storage/{vf_index}/{vf_id}/{vf_version}.rf"; +pub const SERVER_FILE_VF_META: &str = "./storage/{vf_index}/{vf_id}/meta.vf"; +pub const SERVER_NAME_VF_META: &str = "meta.vf"; + +// Server - Updates +pub const SERVER_FILE_UPDATES: &str = "./.updates.txt"; + +// Server - Service +pub const SERVER_FILE_LOCKFILE: &str = "./.lock"; + +// Server - Documents +pub const SERVER_FILE_README: &str = "./README.md"; + +// ------------------------------------------------------------------------------------- + +// Client +pub const CLIENT_PATH_WORKSPACE_ROOT: &str = "./.jv/"; +pub const CLIENT_FOLDER_WORKSPACE_ROOT_NAME: &str = ".jv"; + +// Client - Workspace (Main) +pub const CLIENT_FILE_WORKSPACE: &str = "./.jv/workspace.toml"; + +// Client - Latest Information +pub const CLIENT_FILE_LATEST_INFO: &str = "./.jv/latest/{account}.up"; +pub const CLIENT_FILE_LATEST_DATA: &str = "./.jv/latest/{account}.upf"; + +// Client - Local +pub const CLIENT_PATH_LOCAL_DRAFT: &str = "./.jv/drafts/{account}/{sheet_name}/"; +pub const CLIENT_PATH_LOCAL_SHEET: &str = "./.jv/sheets/local/"; +pub const CLIENT_FILE_LOCAL_SHEET: &str = "./.jv/sheets/local/{account}/{sheet_name}.lst"; +pub const CLIENT_PATH_CACHED_SHEET: &str = "./.jv/sheets/cached/"; +pub const CLIENT_FILE_CACHED_SHEET: &str = "./.jv/sheets/cached/{sheet_name}.st"; + +pub const CLIENT_FILE_LOCAL_SHEET_NOSET: &str = "./.jv/.temp/wrong.json"; +pub const CLIENT_FILE_MEMBER_HELD_NOSET: &str = "./.jv/.temp/wrong.json"; +pub const CLIENT_FILE_LATEST_INFO_NOSET: &str = "./.jv/.temp/wrong.json"; + +// Client - Other +pub const CLIENT_FILE_IGNOREFILES: &str = "IGNORE_RULES.toml"; +pub const CLIENT_FILE_TODOLIST: &str = "./SETUP.md"; +pub const CLIENT_FILE_GITIGNORE: &str = "./.jv/.gitignore"; +pub const CLIENT_CONTENT_GITIGNORE: &str = "# Git support for JVCS Workspace + +# Ignore cached datas +/sheets/cached/ +/latest/ + +.vault_modified"; +pub const CLIENT_FILE_VAULT_MODIFIED: &str = "./.jv/.vault_modified"; +pub const CLIENT_FILE_TEMP_FILE: &str = "./.jv/.temp/download/{temp_name}"; + +// ------------------------------------------------------------------------------------- + +// User - Verify (Documents path) +pub const USER_FILE_ACCOUNTS: &str = "./accounts/"; +pub const USER_FILE_KEY: &str = "./accounts/{self_id}_private.pem"; +pub const USER_FILE_MEMBER: &str = "./accounts/{self_id}.toml"; diff --git a/legacy_data/src/data.rs b/legacy_data/src/data.rs new file mode 100644 index 0000000..ed9383a --- /dev/null +++ b/legacy_data/src/data.rs @@ -0,0 +1,5 @@ +pub mod local; +pub mod member; +pub mod sheet; +pub mod user; +pub mod vault; diff --git a/legacy_data/src/data/local.rs b/legacy_data/src/data/local.rs new file mode 100644 index 0000000..d4115c6 --- /dev/null +++ b/legacy_data/src/data/local.rs @@ -0,0 +1,266 @@ +use std::{ + collections::HashMap, + env::current_dir, + path::{Path, PathBuf}, + sync::Arc, +}; + +use cfg_file::config::ConfigFile; +use string_proc::format_path::format_path; +use tokio::{fs, sync::Mutex}; +use vcs_docs::docs::READMES_LOCAL_WORKSPACE_TODOLIST; + +use crate::{ + constants::{ + CLIENT_CONTENT_GITIGNORE, CLIENT_FILE_GITIGNORE, CLIENT_FILE_LOCAL_SHEET, + CLIENT_FILE_TODOLIST, CLIENT_FILE_WORKSPACE, CLIENT_FOLDER_WORKSPACE_ROOT_NAME, + CLIENT_PATH_LOCAL_SHEET, CLIENT_SUFFIX_LOCAL_SHEET_FILE, KEY_ACCOUNT, KEY_SHEET_NAME, + }, + data::{ + local::{ + local_sheet::{LocalSheet, LocalSheetData, LocalSheetPathBuf}, + workspace_config::LocalConfig, + }, + member::MemberId, + sheet::SheetName, + }, + env::{current_local_path, find_local_path}, +}; + +pub mod align_tasks; +pub mod cached_sheet; +pub mod latest_file_data; +pub mod latest_info; +pub mod local_files; +pub mod local_sheet; +pub mod modified_status; +pub mod workspace_analyzer; +pub mod workspace_config; + +pub struct LocalWorkspace { + config: Arc>, + local_path: PathBuf, +} + +impl LocalWorkspace { + /// Get the path of the local workspace. + pub fn local_path(&self) -> &PathBuf { + &self.local_path + } + + /// Initialize local workspace. + pub fn init(config: LocalConfig, local_path: impl Into) -> Option { + let local_path = find_local_path(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: Arc::new(Mutex::new(config)), + local_path, + }) + } + + /// Setup local workspace + pub async fn setup_local_workspace( + local_path: impl Into, + ) -> Result<(), std::io::Error> { + let local_path: PathBuf = local_path.into(); + + // Ensure directory is empty + if local_path.exists() && local_path.read_dir()?.next().is_some() { + return Err(std::io::Error::new( + std::io::ErrorKind::DirectoryNotEmpty, + "DirectoryNotEmpty", + )); + } + + // 1. Setup config + let config = LocalConfig::default(); + LocalConfig::write_to(&config, local_path.join(CLIENT_FILE_WORKSPACE)).await?; + + // 2. Setup SETUP.md + let readme_content = READMES_LOCAL_WORKSPACE_TODOLIST.trim().to_string(); + fs::write(local_path.join(CLIENT_FILE_TODOLIST), readme_content).await?; + + // 3. Setup .gitignore + fs::write( + local_path.join(CLIENT_FILE_GITIGNORE), + CLIENT_CONTENT_GITIGNORE, + ) + .await?; + + // On Windows, set the .jv directory as hidden + let jv_dir = local_path.join(CLIENT_FOLDER_WORKSPACE_ROOT_NAME); + let _ = hide_folder::hide_folder(&jv_dir); + + 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?; + Ok(()) + } + + /// Get the path to a local sheet. + pub fn local_sheet_path(&self, member: &MemberId, sheet: &SheetName) -> PathBuf { + self.local_path.join( + CLIENT_FILE_LOCAL_SHEET + .replace(KEY_ACCOUNT, member) + .replace(KEY_SHEET_NAME, sheet), + ) + } + + /// Read or initialize a local sheet. + pub async fn local_sheet( + &self, + member: &MemberId, + sheet: &SheetName, + ) -> Result, std::io::Error> { + let local_sheet_path = self.local_sheet_path(member, sheet); + + if !local_sheet_path.exists() { + let sheet_data = LocalSheetData { + mapping: HashMap::new(), + vfs: HashMap::new(), + }; + LocalSheetData::write_to(&sheet_data, local_sheet_path).await?; + return Ok(LocalSheet { + local_workspace: self, + member: member.clone(), + sheet_name: sheet.clone(), + data: sheet_data, + }); + } + + let data = LocalSheetData::read_from(&local_sheet_path).await?; + let local_sheet = LocalSheet { + local_workspace: self, + member: member.clone(), + sheet_name: sheet.clone(), + data, + }; + + Ok(local_sheet) + } + + /// Collect all theet names + pub async fn local_sheet_paths(&self) -> Result, std::io::Error> { + let local_sheet_path = self.local_path.join(CLIENT_PATH_LOCAL_SHEET); + let mut sheet_paths = Vec::new(); + + async fn collect_sheet_paths( + dir: &Path, + suffix: &str, + paths: &mut Vec, + ) -> Result<(), std::io::Error> { + if dir.is_dir() { + let mut entries = fs::read_dir(dir).await?; + while let Some(entry) = entries.next_entry().await? { + let path = entry.path(); + + if path.is_dir() { + Box::pin(collect_sheet_paths(&path, suffix, paths)).await?; + } else if path.is_file() + && let Some(extension) = path.extension() + && extension == suffix.trim_start_matches('.') + { + let formatted_path = format_path(path)?; + paths.push(formatted_path); + } + } + } + Ok(()) + } + + collect_sheet_paths( + &local_sheet_path, + CLIENT_SUFFIX_LOCAL_SHEET_FILE, + &mut sheet_paths, + ) + .await?; + Ok(sheet_paths) + } +} + +mod hide_folder { + use std::io; + use std::path::Path; + + #[cfg(windows)] + use std::os::windows::ffi::OsStrExt; + #[cfg(windows)] + use winapi::um::fileapi::{GetFileAttributesW, INVALID_FILE_ATTRIBUTES, SetFileAttributesW}; + + pub fn hide_folder(path: &Path) -> io::Result<()> { + if !path.is_dir() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Path must be a directory", + )); + } + + if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) { + if !file_name.starts_with('.') { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Directory name must start with '.'", + )); + } + } else { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Invalid directory name", + )); + } + + hide_folder_impl(path) + } + + #[cfg(windows)] + fn hide_folder_impl(path: &Path) -> io::Result<()> { + // Convert to Windows wide string format + let path_str: Vec = path.as_os_str().encode_wide().chain(Some(0)).collect(); + + // Get current attributes + let attrs = unsafe { GetFileAttributesW(path_str.as_ptr()) }; + if attrs == INVALID_FILE_ATTRIBUTES { + return Err(io::Error::last_os_error()); + } + + // Add hidden attribute flag + let new_attrs = attrs | winapi::um::winnt::FILE_ATTRIBUTE_HIDDEN; + + // Set new attributes + let success = unsafe { SetFileAttributesW(path_str.as_ptr(), new_attrs) }; + if success == 0 { + return Err(io::Error::last_os_error()); + } + + Ok(()) + } + + #[cfg(unix)] + fn hide_folder_impl(_path: &Path) -> io::Result<()> { + Ok(()) + } + + #[cfg(not(any(windows, unix)))] + fn hide_folder_impl(_path: &Path) -> io::Result<()> { + Err(io::Error::new( + io::ErrorKind::Unsupported, + "Unsupported operating system", + )) + } +} diff --git a/legacy_data/src/data/local/align_tasks.rs b/legacy_data/src/data/local/align_tasks.rs new file mode 100644 index 0000000..b72804c --- /dev/null +++ b/legacy_data/src/data/local/align_tasks.rs @@ -0,0 +1,110 @@ +use std::{ + collections::{HashMap, HashSet}, + path::PathBuf, +}; + +use data_struct::data_sort::quick_sort_with_cmp; + +use crate::data::local::workspace_analyzer::AnalyzeResult; + +pub type AlignTaskName = String; +pub type AlignPathBuf = PathBuf; +pub type AlignLostPathBuf = PathBuf; +pub type AlignCreatedPathBuf = PathBuf; + +pub struct AlignTasks { + pub created: Vec<(AlignTaskName, AlignPathBuf)>, + pub lost: Vec<(AlignTaskName, AlignPathBuf)>, + pub moved: Vec<(AlignTaskName, (AlignLostPathBuf, AlignCreatedPathBuf))>, + pub erased: Vec<(AlignTaskName, AlignPathBuf)>, +} + +impl AlignTasks { + pub fn clone_from_analyze_result(result: &AnalyzeResult) -> Self { + AlignTasks { + created: path_hash_set_sort_helper(result.created.clone(), "created"), + lost: path_hash_set_sort_helper(result.lost.clone(), "lost"), + moved: path_hash_map_sort_helper(result.moved.clone(), "moved"), + erased: path_hash_set_sort_helper(result.erased.clone(), "erased"), + } + } + + pub fn from_analyze_result(result: AnalyzeResult) -> Self { + AlignTasks { + created: path_hash_set_sort_helper(result.created, "created"), + lost: path_hash_set_sort_helper(result.lost, "lost"), + moved: path_hash_map_sort_helper(result.moved, "moved"), + erased: path_hash_set_sort_helper(result.erased, "erased"), + } + } +} + +fn path_hash_set_sort_helper( + hash_set: HashSet, + prefix: impl Into, +) -> Vec<(String, PathBuf)> { + let prefix_str = prefix.into(); + let mut vec: Vec<(String, PathBuf)> = hash_set + .into_iter() + .map(|path| { + let hash = sha1_hash::calc_sha1_string(path.to_string_lossy()); + let hash_prefix: String = hash.chars().take(8).collect(); + let name = format!("{}:{}", prefix_str, hash_prefix); + (name, path) + }) + .collect(); + + quick_sort_with_cmp(&mut vec, false, |a, b| { + // Compare by path depth first + let a_depth = a.1.components().count(); + let b_depth = b.1.components().count(); + + if a_depth != b_depth { + return if a_depth < b_depth { -1 } else { 1 }; + } + + // If same depth, compare lexicographically + match a.1.cmp(&b.1) { + std::cmp::Ordering::Less => -1, + std::cmp::Ordering::Equal => 0, + std::cmp::Ordering::Greater => 1, + } + }); + + vec +} + +fn path_hash_map_sort_helper( + hash_map: HashMap, + prefix: impl Into, +) -> Vec<(String, (PathBuf, PathBuf))> { + let prefix_str = prefix.into(); + let mut vec: Vec<(String, (PathBuf, PathBuf))> = hash_map + .into_values() + .map(|(path1, path2)| { + let hash = sha1_hash::calc_sha1_string(path1.to_string_lossy()); + let hash_prefix: String = hash.chars().take(8).collect(); + let name = format!("{}:{}", prefix_str, hash_prefix); + (name, (path1, path2)) + }) + .collect(); + + quick_sort_with_cmp(&mut vec, false, |a, b| { + // Compare by first PathBuf's path depth first + let a_depth = a.1.0.components().count(); + let b_depth = b.1.0.components().count(); + + if a_depth != b_depth { + return if a_depth < b_depth { -1 } else { 1 }; + } + + // If same depth, compare lexicographically by first PathBuf + match a.1.0.cmp(&b.1.0) { + std::cmp::Ordering::Less => -1, + std::cmp::Ordering::Equal => 0, + std::cmp::Ordering::Greater => 1, + } + }); + + vec +} diff --git a/legacy_data/src/data/local/cached_sheet.rs b/legacy_data/src/data/local/cached_sheet.rs new file mode 100644 index 0000000..46b390f --- /dev/null +++ b/legacy_data/src/data/local/cached_sheet.rs @@ -0,0 +1,94 @@ +use std::{io::Error, path::PathBuf}; + +use cfg_file::config::ConfigFile; +use string_proc::{format_path::format_path, snake_case}; +use tokio::fs; + +use crate::{ + constants::{ + CLIENT_FILE_CACHED_SHEET, CLIENT_PATH_CACHED_SHEET, CLIENT_SUFFIX_CACHED_SHEET_FILE, + KEY_SHEET_NAME, + }, + data::sheet::{SheetData, SheetName}, + env::current_local_path, +}; + +pub type CachedSheetPathBuf = PathBuf; + +/// # Cached Sheet +/// The cached sheet is a read-only version cloned from the upstream repository to the local environment, +/// automatically generated during update operations, +/// which records the latest Sheet information stored locally to accelerate data access and reduce network requests. +pub struct CachedSheet; + +impl CachedSheet { + /// Read the cached sheet data. + pub async fn cached_sheet_data(sheet_name: &SheetName) -> Result { + let sheet_name = snake_case!(sheet_name.clone()); + + let Some(path) = Self::cached_sheet_path(sheet_name) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Local workspace not found!", + )); + }; + let data = SheetData::read_from(path).await?; + Ok(data) + } + + /// Get the path to the cached sheet file. + pub fn cached_sheet_path(sheet_name: SheetName) -> Option { + let current_workspace = current_local_path()?; + Some( + current_workspace + .join(CLIENT_FILE_CACHED_SHEET.replace(KEY_SHEET_NAME, &sheet_name.to_string())), + ) + } + + /// Get all cached sheet names + pub async fn cached_sheet_names() -> Result, std::io::Error> { + let mut dir = fs::read_dir(CLIENT_PATH_CACHED_SHEET).await?; + let mut sheet_names = Vec::new(); + + while let Some(entry) = dir.next_entry().await? { + let path = entry.path(); + + if path.is_file() + && let Some(file_name) = path.file_name().and_then(|n| n.to_str()) + && file_name.ends_with(CLIENT_SUFFIX_CACHED_SHEET_FILE) + { + let name_without_ext = file_name + .trim_end_matches(CLIENT_SUFFIX_CACHED_SHEET_FILE) + .to_string(); + sheet_names.push(name_without_ext); + } + } + + Ok(sheet_names) + } + + /// Get all cached sheet paths + pub async fn cached_sheet_paths() -> Result, std::io::Error> { + let mut dir = fs::read_dir(CLIENT_PATH_CACHED_SHEET).await?; + let mut sheet_paths = Vec::new(); + let Some(workspace_path) = current_local_path() else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Local workspace not found!", + )); + }; + + while let Some(entry) = dir.next_entry().await? { + let path = entry.path(); + + if path.is_file() + && let Some(file_name) = path.file_name().and_then(|n| n.to_str()) + && file_name.ends_with(CLIENT_SUFFIX_CACHED_SHEET_FILE) + { + sheet_paths.push(format_path(workspace_path.join(path))?); + } + } + + Ok(sheet_paths) + } +} diff --git a/legacy_data/src/data/local/latest_file_data.rs b/legacy_data/src/data/local/latest_file_data.rs new file mode 100644 index 0000000..f9b3aeb --- /dev/null +++ b/legacy_data/src/data/local/latest_file_data.rs @@ -0,0 +1,103 @@ +use std::{collections::HashMap, io::Error, path::PathBuf}; + +use cfg_file::ConfigFile; +use serde::{Deserialize, Serialize}; + +use crate::{ + constants::{CLIENT_FILE_LATEST_DATA, CLIENT_FILE_MEMBER_HELD_NOSET, KEY_ACCOUNT}, + data::{ + member::MemberId, + vault::virtual_file::{VirtualFileId, VirtualFileVersion, VirtualFileVersionDescription}, + }, + env::current_local_path, +}; + +/// # Latest file data +/// Records the file holder and the latest version for permission and update checks +#[derive(Debug, Default, Clone, Serialize, Deserialize, ConfigFile)] +#[cfg_file(path = CLIENT_FILE_MEMBER_HELD_NOSET)] +pub struct LatestFileData { + /// File holding status + #[serde(rename = "held")] + held_status: HashMap, + + /// File version + #[serde(rename = "ver")] + versions: HashMap, + + /// File histories and descriptions + #[serde(rename = "his")] + histories: HashMap>, +} + +#[derive(Debug, Default, Clone, Serialize, Deserialize)] +pub enum HeldStatus { + #[serde(rename = "Hold")] + HeldWith(MemberId), // Held, status changes are sync to the client + + #[serde(rename = "None")] + NotHeld, // Not held, status changes are sync to the client + + #[default] + #[serde(rename = "Unknown")] + WantedToKnow, // Holding status is unknown, notify server must inform client +} + +impl LatestFileData { + /// Get the path to the file holding the held status information for the given member. + pub fn data_path(account: &MemberId) -> Result { + let Some(local_path) = current_local_path() else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Workspace not found.", + )); + }; + Ok(local_path.join(CLIENT_FILE_LATEST_DATA.replace(KEY_ACCOUNT, account))) + } + + /// Get the member who holds the file with the given ID. + pub fn file_holder(&self, vfid: &VirtualFileId) -> Option<&MemberId> { + self.held_status.get(vfid).and_then(|status| match status { + HeldStatus::HeldWith(id) => Some(id), + _ => None, + }) + } + + /// Get the version of the file with the given ID. + pub fn file_version(&self, vfid: &VirtualFileId) -> Option<&VirtualFileVersion> { + self.versions.get(vfid) + } + + /// Get the version of the file with the given ID. + pub fn file_histories( + &self, + vfid: &VirtualFileId, + ) -> Option<&Vec<(VirtualFileVersion, VirtualFileVersionDescription)>> { + self.histories.get(vfid) + } + + /// Update the held status of the files. + pub fn update_info( + &mut self, + map: HashMap< + VirtualFileId, + ( + Option, + VirtualFileVersion, + Vec<(VirtualFileVersion, VirtualFileVersionDescription)>, + ), + >, + ) { + for (vfid, (member_id, version, desc)) in map { + self.held_status.insert( + vfid.clone(), + match member_id { + Some(member_id) => HeldStatus::HeldWith(member_id), + None => HeldStatus::NotHeld, + }, + ); + self.versions.insert(vfid.clone(), version); + self.histories.insert(vfid, desc); + } + } +} diff --git a/legacy_data/src/data/local/latest_info.rs b/legacy_data/src/data/local/latest_info.rs new file mode 100644 index 0000000..5748793 --- /dev/null +++ b/legacy_data/src/data/local/latest_info.rs @@ -0,0 +1,81 @@ +use std::{ + collections::{HashMap, HashSet}, + path::{Path, PathBuf}, + time::SystemTime, +}; + +use cfg_file::ConfigFile; +use serde::{Deserialize, Serialize}; + +use crate::{ + constants::{CLIENT_FILE_LATEST_INFO, CLIENT_FILE_LATEST_INFO_NOSET, KEY_ACCOUNT}, + data::{ + member::{Member, MemberId}, + sheet::{SheetData, SheetName, SheetPathBuf}, + vault::{ + mapping_share::{Share, SheetShareId}, + virtual_file::VirtualFileId, + }, + }, +}; + +/// # Latest Info +/// Locally cached latest information, +/// used to cache personal information from upstream for querying and quickly retrieving member information. +#[derive(Default, Serialize, Deserialize, ConfigFile)] +#[cfg_file(path = CLIENT_FILE_LATEST_INFO_NOSET)] +pub struct LatestInfo { + // Sheets + /// Visible sheets, + /// indicating which sheets I can edit + #[serde(rename = "my")] + pub visible_sheets: Vec, + + /// Invisible sheets, + /// indicating which sheets I can export files to (these sheets are not readable to me) + #[serde(rename = "others")] + pub invisible_sheets: Vec, + + /// Reference sheets, + /// indicating sheets owned by the host, visible to everyone, + /// but only the host can modify or add mappings within them + #[serde(rename = "refsheets")] + pub reference_sheets: HashSet, + + /// Reference sheet data, indicating what files I can get from the reference sheet + #[serde(rename = "ref")] + pub ref_sheet_content: SheetData, + + /// Reverse mapping from virtual file IDs to actual paths in reference sheets + #[serde(rename = "ref_vfs")] + pub ref_sheet_vfs_mapping: HashMap, + + /// Shares in my sheets, indicating which external merge requests have entries that I can view + #[serde(rename = "shares")] + pub shares_in_my_sheets: HashMap>, + + /// Update instant + #[serde(rename = "update")] + pub update_instant: Option, + + // Members + /// All member information of the vault, allowing me to contact them more conveniently + #[serde(rename = "members")] + pub vault_members: Vec, +} + +impl LatestInfo { + /// Get the path to the latest info file for a given workspace and member ID + pub fn latest_info_path(local_workspace_path: &Path, member_id: &MemberId) -> PathBuf { + local_workspace_path.join(CLIENT_FILE_LATEST_INFO.replace(KEY_ACCOUNT, member_id)) + } +} + +#[derive(Default, Serialize, Deserialize)] +pub struct SheetInfo { + #[serde(rename = "name")] + pub sheet_name: SheetName, + + #[serde(rename = "holder")] + pub holder_name: Option, +} diff --git a/legacy_data/src/data/local/local_files.rs b/legacy_data/src/data/local/local_files.rs new file mode 100644 index 0000000..9cc244f --- /dev/null +++ b/legacy_data/src/data/local/local_files.rs @@ -0,0 +1,148 @@ +use std::path::{Path, PathBuf}; + +use string_proc::format_path::format_path; +use tokio::fs; + +use crate::constants::CLIENT_FOLDER_WORKSPACE_ROOT_NAME; + +pub struct RelativeFiles { + pub(crate) files: Vec, +} + +impl IntoIterator for RelativeFiles { + type Item = PathBuf; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.files.into_iter() + } +} + +impl RelativeFiles { + pub fn iter(&self) -> std::slice::Iter<'_, PathBuf> { + self.files.iter() + } +} + +/// Read the relative paths within the project from the input file list +pub async fn get_relative_paths(local_path: &PathBuf, paths: &[PathBuf]) -> Option { + // Get Relative Paths + let Ok(paths) = format_input_paths_and_ignore_outside_paths(local_path, paths).await else { + return None; + }; + let files: Vec = abs_paths_to_abs_files(paths).await; + let Ok(files) = parse_to_relative(local_path, files) else { + return None; + }; + Some(RelativeFiles { files }) +} + +/// Normalize the input paths +async fn format_input_paths( + local_path: &Path, + track_files: &[PathBuf], +) -> Result, std::io::Error> { + let current_dir = local_path; + + let mut real_paths = Vec::new(); + for file in track_files { + let path = current_dir.join(file); + + // Skip paths that contain .jv directories + if path.components().any(|component| { + if let std::path::Component::Normal(name) = component { + name.to_str() == Some(CLIENT_FOLDER_WORKSPACE_ROOT_NAME) + } else { + false + } + }) { + continue; + } + + match format_path(path) { + Ok(path) => real_paths.push(path), + Err(e) => { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!("Failed to format path: {}", e), + )); + } + } + } + + Ok(real_paths) +} + +/// Ignore files outside the workspace +async fn format_input_paths_and_ignore_outside_paths( + local_path: &PathBuf, + files: &[PathBuf], +) -> Result, std::io::Error> { + let result = format_input_paths(local_path, files).await?; + let result: Vec = result + .into_iter() + .filter(|path| path.starts_with(local_path)) + .collect(); + Ok(result) +} + +/// Normalize the input paths to relative paths +fn parse_to_relative( + local_dir: &PathBuf, + files: Vec, +) -> Result, std::io::Error> { + let result: Result, _> = files + .iter() + .map(|p| { + p.strip_prefix(local_dir) + .map(|relative| relative.to_path_buf()) + .map_err(|_| { + std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "Path prefix stripping failed", + ) + }) + }) + .collect(); + + result +} + +/// Convert absolute paths to absolute file paths, expanding directories to their contained files +async fn abs_paths_to_abs_files(paths: Vec) -> Vec { + let mut files = Vec::new(); + + for path in paths { + if !path.exists() { + continue; + } + + let metadata = match fs::metadata(&path).await { + Ok(meta) => meta, + Err(_) => continue, + }; + + if metadata.is_file() { + files.push(path); + } else if metadata.is_dir() { + let walker = walkdir::WalkDir::new(&path); + for entry in walker.into_iter().filter_map(|e| e.ok()) { + if entry.path().components().any(|component| { + if let std::path::Component::Normal(name) = component { + name == CLIENT_FOLDER_WORKSPACE_ROOT_NAME + } else { + false + } + }) { + continue; + } + + if entry.file_type().is_file() { + files.push(entry.path().to_path_buf()); + } + } + } + } + + files +} diff --git a/legacy_data/src/data/local/local_sheet.rs b/legacy_data/src/data/local/local_sheet.rs new file mode 100644 index 0000000..b9c29f5 --- /dev/null +++ b/legacy_data/src/data/local/local_sheet.rs @@ -0,0 +1,439 @@ +use std::{collections::HashMap, io::Error, path::PathBuf, time::SystemTime}; + +use ::serde::{Deserialize, Serialize}; +use cfg_file::{ConfigFile, config::ConfigFile}; +use string_proc::format_path::format_path; + +use crate::{ + constants::CLIENT_FILE_LOCAL_SHEET_NOSET, + data::{ + local::LocalWorkspace, + member::MemberId, + sheet::SheetName, + vault::virtual_file::{VirtualFileId, VirtualFileVersion, VirtualFileVersionDescription}, + }, +}; + +pub type LocalFilePathBuf = PathBuf; +pub type LocalSheetPathBuf = PathBuf; + +/// # Local Sheet +/// Local sheet information, used to record metadata of actual local files, +/// to compare with upstream information for more optimized file submission, +/// and to determine whether files need to be updated or submitted. +pub struct LocalSheet<'a> { + pub(crate) local_workspace: &'a LocalWorkspace, + pub(crate) member: MemberId, + pub(crate) sheet_name: String, + pub(crate) data: LocalSheetData, +} + +impl<'a> LocalSheet<'a> { + /// Create a new LocalSheet instance + pub fn new( + local_workspace: &'a LocalWorkspace, + member: MemberId, + sheet_name: String, + data: LocalSheetData, + ) -> Self { + Self { + local_workspace, + member, + sheet_name, + data, + } + } +} + +#[derive(Debug, Default, Serialize, Deserialize, ConfigFile, Clone)] +#[cfg_file(path = CLIENT_FILE_LOCAL_SHEET_NOSET)] // Do not use LocalSheet::write or LocalSheet::read +pub struct LocalSheetData { + /// Local file path to metadata mapping. + #[serde(rename = "map")] + pub(crate) mapping: HashMap, + + #[serde(rename = "vfs")] + pub(crate) vfs: HashMap, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct LocalMappingMetadata { + /// Hash value generated immediately after the file is downloaded to the local workspace + #[serde(rename = "base_hash")] + pub(crate) hash_when_updated: String, + + /// Time when the file was downloaded to the local workspace + #[serde(rename = "time")] + pub(crate) time_when_updated: SystemTime, + + /// Size of the file when downloaded to the local workspace + #[serde(rename = "size")] + pub(crate) size_when_updated: u64, + + /// Version description when the file was downloaded to the local workspace + #[serde(rename = "desc")] + pub(crate) version_desc_when_updated: VirtualFileVersionDescription, + + /// Version when the file was downloaded to the local workspace + #[serde(rename = "ver")] + pub(crate) version_when_updated: VirtualFileVersion, + + /// Virtual file ID corresponding to the local path + #[serde(rename = "id")] + pub(crate) mapping_vfid: VirtualFileId, + + /// Latest modifiy check time + #[serde(rename = "check_time")] + pub(crate) last_modify_check_time: SystemTime, + + /// Latest modifiy check result + #[serde(rename = "modified")] + pub(crate) last_modify_check_result: bool, + + /// Latest modifiy check hash result + #[serde(rename = "current_hash")] + pub(crate) last_modify_check_hash: Option, +} + +impl LocalSheetData { + /// Wrap LocalSheetData into LocalSheet with workspace, member, and sheet name + pub fn wrap_to_local_sheet<'a>( + self, + workspace: &'a LocalWorkspace, + member: MemberId, + sheet_name: SheetName, + ) -> LocalSheet<'a> { + LocalSheet { + local_workspace: workspace, + member, + sheet_name, + data: self, + } + } +} + +impl LocalMappingMetadata { + /// Create a new MappingMetaData instance + #[allow(clippy::too_many_arguments)] + pub fn new( + hash_when_updated: String, + time_when_updated: SystemTime, + size_when_updated: u64, + version_desc_when_updated: VirtualFileVersionDescription, + version_when_updated: VirtualFileVersion, + mapping_vfid: VirtualFileId, + last_modifiy_check_time: SystemTime, + last_modifiy_check_result: bool, + ) -> Self { + Self { + hash_when_updated, + time_when_updated, + size_when_updated, + version_desc_when_updated, + version_when_updated, + mapping_vfid, + last_modify_check_time: last_modifiy_check_time, + last_modify_check_result: last_modifiy_check_result, + last_modify_check_hash: None, + } + } + + /// Getter for hash_when_updated + pub fn hash_when_updated(&self) -> &String { + &self.hash_when_updated + } + + /// Setter for hash_when_updated + pub fn set_hash_when_updated(&mut self, hash: String) { + self.hash_when_updated = hash; + } + + /// Getter for date_when_updated + pub fn time_when_updated(&self) -> &SystemTime { + &self.time_when_updated + } + + /// Setter for time_when_updated + pub fn set_time_when_updated(&mut self, time: SystemTime) { + self.time_when_updated = time; + } + + /// Getter for size_when_updated + pub fn size_when_updated(&self) -> u64 { + self.size_when_updated + } + + /// Setter for size_when_updated + pub fn set_size_when_updated(&mut self, size: u64) { + self.size_when_updated = size; + } + + /// Getter for version_desc_when_updated + pub fn version_desc_when_updated(&self) -> &VirtualFileVersionDescription { + &self.version_desc_when_updated + } + + /// Setter for version_desc_when_updated + pub fn set_version_desc_when_updated(&mut self, version_desc: VirtualFileVersionDescription) { + self.version_desc_when_updated = version_desc; + } + + /// Getter for version_when_updated + pub fn version_when_updated(&self) -> &VirtualFileVersion { + &self.version_when_updated + } + + /// Setter for version_when_updated + pub fn set_version_when_updated(&mut self, version: VirtualFileVersion) { + self.version_when_updated = version; + } + + /// Getter for mapping_vfid + pub fn mapping_vfid(&self) -> &VirtualFileId { + &self.mapping_vfid + } + + /// Setter for mapping_vfid + pub fn set_mapping_vfid(&mut self, vfid: VirtualFileId) { + self.mapping_vfid = vfid; + } + + /// Getter for last_modifiy_check_time + pub fn last_modifiy_check_time(&self) -> &SystemTime { + &self.last_modify_check_time + } + + /// Setter for last_modifiy_check_time + pub fn set_last_modifiy_check_time(&mut self, time: SystemTime) { + self.last_modify_check_time = time; + } + + /// Getter for last_modifiy_check_result + pub fn last_modifiy_check_result(&self) -> bool { + self.last_modify_check_result + } + + /// Setter for last_modifiy_check_result + pub fn set_last_modifiy_check_result(&mut self, result: bool) { + self.last_modify_check_result = result; + } + + /// Getter for last_modifiy_check_hash + pub fn last_modifiy_check_hash(&self) -> &Option { + &self.last_modify_check_hash + } + + /// Setter for last_modifiy_check_hash + pub fn set_last_modifiy_check_hash(&mut self, hash: Option) { + self.last_modify_check_hash = hash; + } +} + +impl Default for LocalMappingMetadata { + fn default() -> Self { + Self { + hash_when_updated: Default::default(), + time_when_updated: SystemTime::now(), + size_when_updated: Default::default(), + version_desc_when_updated: Default::default(), + version_when_updated: Default::default(), + mapping_vfid: Default::default(), + last_modify_check_time: SystemTime::now(), + last_modify_check_result: false, + last_modify_check_hash: None, + } + } +} + +mod instant_serde { + use serde::{self, Deserialize, Deserializer, Serializer}; + use tokio::time::Instant; + + pub fn serialize(instant: &Instant, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_u64(instant.elapsed().as_secs()) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let secs = u64::deserialize(deserializer)?; + Ok(Instant::now() - std::time::Duration::from_secs(secs)) + } +} + +impl<'a> From<&'a LocalSheet<'a>> for &'a LocalSheetData { + fn from(sheet: &'a LocalSheet<'a>) -> Self { + &sheet.data + } +} + +impl LocalSheetData { + /// Add mapping to local sheet data + pub fn add_mapping( + &mut self, + path: &LocalFilePathBuf, + mapping: LocalMappingMetadata, + ) -> Result<(), std::io::Error> { + let path = format_path(path)?; + if self.mapping.contains_key(&path) || self.vfs.contains_key(&mapping.mapping_vfid) { + return Err(Error::new( + std::io::ErrorKind::AlreadyExists, + "Mapping already exists", + )); + } + + self.mapping.insert(path.clone(), mapping.clone()); + self.vfs.insert(mapping.mapping_vfid.clone(), path); + Ok(()) + } + + /// Move mapping to other path + pub fn move_mapping( + &mut self, + from: &LocalFilePathBuf, + to: &LocalFilePathBuf, + ) -> Result<(), std::io::Error> { + let from = format_path(from)?; + let to = format_path(to)?; + if self.mapping.contains_key(&to) { + return Err(Error::new( + std::io::ErrorKind::AlreadyExists, + "To path already exists.", + )); + } + + let Some(old_value) = self.mapping.remove(&from) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "From path is not found.", + )); + }; + + // Update vfs mapping + self.vfs.insert(old_value.mapping_vfid.clone(), to.clone()); + self.mapping.insert(to, old_value); + + Ok(()) + } + + /// Remove mapping from local sheet + pub fn remove_mapping( + &mut self, + path: &LocalFilePathBuf, + ) -> Result { + let path = format_path(path)?; + match self.mapping.remove(&path) { + Some(mapping) => { + self.vfs.remove(&mapping.mapping_vfid); + Ok(mapping) + } + None => Err(Error::new( + std::io::ErrorKind::NotFound, + "Path is not found.", + )), + } + } + + /// Get immutable mapping data + pub fn mapping_data( + &self, + path: &LocalFilePathBuf, + ) -> Result<&LocalMappingMetadata, std::io::Error> { + let path = format_path(path)?; + let Some(data) = self.mapping.get(&path) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Path is not found.", + )); + }; + Ok(data) + } + + /// Get mutable mapping data + pub fn mapping_data_mut( + &mut self, + path: &LocalFilePathBuf, + ) -> Result<&mut LocalMappingMetadata, std::io::Error> { + let path = format_path(path)?; + let Some(data) = self.mapping.get_mut(&path) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Path is not found.", + )); + }; + Ok(data) + } + + /// Get path by VirtualFileId + pub fn path_by_id(&self, vfid: &VirtualFileId) -> Option<&PathBuf> { + self.vfs.get(vfid) + } +} + +impl<'a> LocalSheet<'a> { + /// Add mapping to local sheet data + pub fn add_mapping( + &mut self, + path: &LocalFilePathBuf, + mapping: LocalMappingMetadata, + ) -> Result<(), std::io::Error> { + self.data.add_mapping(path, mapping) + } + + /// Move mapping to other path + pub fn move_mapping( + &mut self, + from: &LocalFilePathBuf, + to: &LocalFilePathBuf, + ) -> Result<(), std::io::Error> { + self.data.move_mapping(from, to) + } + + /// Remove mapping from local sheet + pub fn remove_mapping( + &mut self, + path: &LocalFilePathBuf, + ) -> Result { + self.data.remove_mapping(path) + } + + /// Get immutable mapping data + pub fn mapping_data( + &self, + path: &LocalFilePathBuf, + ) -> Result<&LocalMappingMetadata, std::io::Error> { + self.data.mapping_data(path) + } + + /// Get mutable mapping data + pub fn mapping_data_mut( + &mut self, + path: &LocalFilePathBuf, + ) -> Result<&mut LocalMappingMetadata, std::io::Error> { + self.data.mapping_data_mut(path) + } + + /// Write the sheet to disk + pub async fn write(&mut self) -> Result<(), std::io::Error> { + let path = self + .local_workspace + .local_sheet_path(&self.member, &self.sheet_name); + self.write_to_path(path).await + } + + /// Write the sheet to custom path + pub async fn write_to_path(&mut self, path: impl Into) -> Result<(), std::io::Error> { + let path = path.into(); + LocalSheetData::write_to(&self.data, path).await?; + Ok(()) + } + + /// Get path by VirtualFileId + pub fn path_by_id(&self, vfid: &VirtualFileId) -> Option<&PathBuf> { + self.data.path_by_id(vfid) + } +} diff --git a/legacy_data/src/data/local/modified_status.rs b/legacy_data/src/data/local/modified_status.rs new file mode 100644 index 0000000..e0e6dd5 --- /dev/null +++ b/legacy_data/src/data/local/modified_status.rs @@ -0,0 +1,30 @@ +use crate::{constants::CLIENT_FILE_VAULT_MODIFIED, env::current_local_path}; + +pub async fn check_vault_modified() -> bool { + let Some(current_dir) = current_local_path() else { + return false; + }; + + let record_file = current_dir.join(CLIENT_FILE_VAULT_MODIFIED); + if !record_file.exists() { + return false; + } + + let Ok(contents) = tokio::fs::read_to_string(&record_file).await else { + return false; + }; + + matches!(contents.trim().to_lowercase().as_str(), "true") +} + +pub async fn sign_vault_modified(modified: bool) { + let Some(current_dir) = current_local_path() else { + return; + }; + + let record_file = current_dir.join(CLIENT_FILE_VAULT_MODIFIED); + + let contents = if modified { "true" } else { "false" }; + + let _ = tokio::fs::write(&record_file, contents).await; +} diff --git a/legacy_data/src/data/local/workspace_analyzer.rs b/legacy_data/src/data/local/workspace_analyzer.rs new file mode 100644 index 0000000..5d73e03 --- /dev/null +++ b/legacy_data/src/data/local/workspace_analyzer.rs @@ -0,0 +1,359 @@ +use std::{ + collections::{HashMap, HashSet}, + io::Error, + path::PathBuf, +}; + +use serde::Serialize; +use sha1_hash::calc_sha1_multi; +use string_proc::format_path::format_path; +use walkdir::WalkDir; + +use crate::data::{ + local::{LocalWorkspace, cached_sheet::CachedSheet, local_sheet::LocalSheet}, + member::MemberId, + sheet::{SheetData, SheetName}, + vault::virtual_file::VirtualFileId, +}; + +pub type FromRelativePathBuf = PathBuf; +pub type ToRelativePathBuf = PathBuf; +pub type CreatedRelativePathBuf = PathBuf; +pub type LostRelativePathBuf = PathBuf; +pub type ModifiedRelativePathBuf = PathBuf; + +pub struct AnalyzeResult<'a> { + local_workspace: &'a LocalWorkspace, + + /// Moved local files + pub moved: HashMap, + + /// Newly created local files + pub created: HashSet, + + /// Lost local files + pub lost: HashSet, + + /// Erased local files + pub erased: HashSet, + + /// Modified local files (excluding moved files) + /// For files that were both moved and modified, changes can only be detected after LocalSheet mapping is aligned with actual files + pub modified: HashSet, +} + +#[derive(Serialize, Default)] +pub struct AnalyzeResultPure { + /// Moved local files + pub moved: HashMap, + + /// Newly created local files + pub created: HashSet, + + /// Lost local files + pub lost: HashSet, + + /// Erased local files + pub erased: HashSet, + + /// Modified local files (excluding moved files) + /// For files that were both moved and modified, changes can only be detected after LocalSheet mapping is aligned with actual files + pub modified: HashSet, +} + +impl<'a> From> for AnalyzeResultPure { + fn from(result: AnalyzeResult<'a>) -> Self { + AnalyzeResultPure { + moved: result.moved, + created: result.created, + lost: result.lost, + erased: result.erased, + modified: result.modified, + } + } +} + +struct AnalyzeContext<'a> { + member: MemberId, + sheet_name: SheetName, + local_sheet: Option>, + cached_sheet_data: Option, +} + +impl<'a> AnalyzeResult<'a> { + /// Analyze all files, calculate the file information provided + pub async fn analyze_local_status( + local_workspace: &'a LocalWorkspace, + ) -> Result, std::io::Error> { + // Workspace + let workspace = local_workspace; + + // Current member, sheet + let (member, sheet_name) = { + let mut_workspace = workspace.config.lock().await; + let member = mut_workspace.current_account(); + let Some(sheet) = mut_workspace.sheet_in_use().clone() else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Sheet not found")); + }; + (member, sheet) + }; + + // Local files (RelativePaths) + let local_path = workspace.local_path(); + let file_relative_paths = { + let mut paths = HashSet::new(); + for entry in WalkDir::new(local_path) { + let entry = match entry { + Ok(entry) => entry, + Err(_) => continue, + }; + + // Skip entries that contain ".jv" in their path + if entry.path().to_string_lossy().contains(".jv") { + continue; + } + + if entry.file_type().is_file() + && let Ok(relative_path) = entry.path().strip_prefix(local_path) + { + let format = format_path(relative_path.to_path_buf()); + let Ok(format) = format else { + continue; + }; + paths.insert(format); + } + } + + paths + }; + + // Read local sheet + let local_sheet = (workspace.local_sheet(&member, &sheet_name).await).ok(); + + // Read cached sheet + let cached_sheet_data = match CachedSheet::cached_sheet_data(&sheet_name).await { + Ok(v) => Some(v), + Err(_) => { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Cached sheet not found", + )); + } + }; + + // Create new result + let mut result = Self::none_result(workspace); + + // Analyze entry + let mut analyze_ctx = AnalyzeContext { + member, + sheet_name, + local_sheet, + cached_sheet_data, + }; + Self::analyze_moved(&mut result, &file_relative_paths, &analyze_ctx, workspace).await?; + Self::analyze_modified( + &mut result, + &file_relative_paths, + &mut analyze_ctx, + workspace, + ) + .await?; + + Ok(result) + } + + /// Track file moves by comparing recorded SHA1 hashes with actual file SHA1 hashes + /// For files that cannot be directly matched, continue searching using fuzzy matching algorithms + async fn analyze_moved( + result: &mut AnalyzeResult<'_>, + file_relative_paths: &HashSet, + analyze_ctx: &AnalyzeContext<'a>, + workspace: &LocalWorkspace, + ) -> Result<(), std::io::Error> { + let local_sheet_paths: HashSet<&PathBuf> = match &analyze_ctx.local_sheet { + Some(local_sheet) => local_sheet.data.mapping.keys().collect(), + None => HashSet::new(), + }; + let file_relative_paths_ref: HashSet<&PathBuf> = file_relative_paths.iter().collect(); + + // Files that exist locally but not in remote + let mut erased_files: HashSet = HashSet::new(); + + if let Some(cached_data) = &analyze_ctx.cached_sheet_data { + if let Some(local_sheet) = &analyze_ctx.local_sheet { + let cached_sheet_mapping = cached_data.mapping(); + let local_sheet_mapping = &local_sheet.data.mapping; + + // Find paths that exist in local sheet but not in cached sheet + for local_path in local_sheet_mapping.keys() { + if !cached_sheet_mapping.contains_key(local_path) { + erased_files.insert(local_path.clone()); + } + } + } + } + + // Files that exist in the local sheet but not in reality are considered lost + let mut lost_files: HashSet<&PathBuf> = local_sheet_paths + .difference(&file_relative_paths_ref) + .filter(|&&path| !erased_files.contains(path)) + .cloned() + .collect(); + + // Files that exist in reality but not in the local sheet are recorded as newly created + let mut new_files: HashSet<&PathBuf> = file_relative_paths_ref + .difference(&local_sheet_paths) + .cloned() + .collect(); + + // Calculate hashes for new files + let new_files_for_hash: Vec = new_files + .iter() + .map(|p| workspace.local_path.join(p)) + .collect(); + let file_hashes: HashSet<(PathBuf, String)> = + match calc_sha1_multi::>(new_files_for_hash, 8192).await { + Ok(hash) => hash, + Err(e) => return Err(Error::other(e)), + } + .iter() + .map(|r| (r.file_path.clone(), r.hash.to_string())) + .collect(); + + // Build hash mapping table for lost files + let mut lost_files_hash_mapping: HashMap = + match &analyze_ctx.local_sheet { + Some(local_sheet) => lost_files + .iter() + .filter_map(|f| { + local_sheet.mapping_data(f).ok().map(|mapping_data| { + ( + // Using the most recently recorded Hash can more accurately identify moved items, + // but if it doesn't exist, fall back to the initially recorded Hash + mapping_data + .last_modify_check_hash + .as_ref() + .cloned() + .unwrap_or(mapping_data.hash_when_updated.clone()), + (*f).clone(), + ) + }) + }) + .collect(), + None => HashMap::new(), + }; + + // If these hashes correspond to the hashes of missing files, then this pair of new and lost items will be merged into moved items + let mut moved_files: HashSet<(FromRelativePathBuf, ToRelativePathBuf)> = HashSet::new(); + for (new_path, new_hash) in file_hashes { + let new_path = new_path + .strip_prefix(&workspace.local_path) + .map(|p| p.to_path_buf()) + .unwrap_or(new_path); + + // If the new hash value hits the mapping, add a moved item + if let Some(lost_path) = lost_files_hash_mapping.remove(&new_hash) { + // Remove this new item and lost item + lost_files.remove(&lost_path); + new_files.remove(&new_path); + + // Create moved item + moved_files.insert((lost_path.clone(), new_path)); + } + } + + // Enter fuzzy matching to match other potentially moved items that haven't been matched + // If the total number of new and lost files is divisible by 2, it indicates there might still be files that have been moved, consider trying fuzzy matching + if new_files.len() + lost_files.len() % 2 == 0 { + // Try fuzzy matching + // ... + } + + // Collect results and set the result + result.created = new_files.iter().map(|p| (*p).clone()).collect(); + result.lost = lost_files.iter().map(|p| (*p).clone()).collect(); + result.moved = moved_files + .iter() + .filter_map(|(from, to)| { + let vfid = analyze_ctx + .local_sheet + .as_ref() + .and_then(|local_sheet| local_sheet.mapping_data(from).ok()) + .map(|mapping_data| mapping_data.mapping_vfid.clone()); + vfid.map(|vfid| (vfid, (from.clone(), to.clone()))) + }) + .collect(); + result.erased = erased_files; + + Ok(()) + } + + /// Compare using file modification time and SHA1 hash values. + /// Note: For files that have been both moved and modified, they can only be recognized as modified after their location is matched. + async fn analyze_modified( + result: &mut AnalyzeResult<'_>, + file_relative_paths: &HashSet, + analyze_ctx: &mut AnalyzeContext<'a>, + workspace: &LocalWorkspace, + ) -> Result<(), std::io::Error> { + let local_sheet = &mut analyze_ctx.local_sheet.as_mut().unwrap(); + let local_path = local_sheet.local_workspace.local_path().clone(); + + for path in file_relative_paths { + // Get mapping data + let Ok(mapping_data) = local_sheet.mapping_data_mut(path) else { + continue; + }; + + // If modified time not changed, skip + let modified_time = std::fs::metadata(local_path.join(path))?.modified()?; + if &modified_time == mapping_data.last_modifiy_check_time() { + if mapping_data.last_modifiy_check_result() { + result.modified.insert(path.clone()); + } + continue; + } + + // Calculate hash + let hash_calc = match sha1_hash::calc_sha1(workspace.local_path.join(path), 2048).await + { + Ok(hash) => hash, + Err(e) => return Err(Error::other(e)), + }; + + // If hash not match, mark as modified + if &hash_calc.hash != mapping_data.hash_when_updated() { + result.modified.insert(path.clone()); + + // Update last modified check time to modified time + mapping_data.last_modify_check_time = modified_time; + mapping_data.last_modify_check_result = true; + } else { + // Update last modified check time to modified time + mapping_data.last_modify_check_time = modified_time; + mapping_data.last_modify_check_result = false; + } + + // Record latest hash + mapping_data.last_modify_check_hash = Some(hash_calc.hash) + } + + // Persist the local sheet data + LocalSheet::write(local_sheet).await?; + + Ok(()) + } + + /// Generate a empty AnalyzeResult + fn none_result(local_workspace: &'a LocalWorkspace) -> AnalyzeResult<'a> { + AnalyzeResult { + local_workspace, + moved: HashMap::new(), + created: HashSet::new(), + lost: HashSet::new(), + modified: HashSet::new(), + erased: HashSet::new(), + } + } +} diff --git a/legacy_data/src/data/local/workspace_config.rs b/legacy_data/src/data/local/workspace_config.rs new file mode 100644 index 0000000..f97d049 --- /dev/null +++ b/legacy_data/src/data/local/workspace_config.rs @@ -0,0 +1,374 @@ +use cfg_file::ConfigFile; +use cfg_file::config::ConfigFile; +use serde::{Deserialize, Serialize}; +use std::io::Error; +use std::net::SocketAddr; +use std::path::Path; +use std::path::PathBuf; +use string_proc::snake_case; + +use crate::constants::CLIENT_FILE_WORKSPACE; +use crate::constants::CLIENT_FOLDER_WORKSPACE_ROOT_NAME; +use crate::constants::CLIENT_PATH_LOCAL_DRAFT; +use crate::constants::CLIENT_PATH_WORKSPACE_ROOT; +use crate::constants::KEY_ACCOUNT; +use crate::constants::KEY_SHEET_NAME; +use crate::constants::PORT; +use crate::data::local::latest_info::LatestInfo; +use crate::data::member::MemberId; +use crate::data::sheet::SheetName; +use crate::data::vault::vault_config::VaultUuid; +use crate::env::current_local_path; + +#[derive(Serialize, Deserialize, ConfigFile, Clone)] +#[cfg_file(path = CLIENT_FILE_WORKSPACE)] +pub struct LocalConfig { + /// The upstream address, representing the upstream address of the local workspace, + /// to facilitate timely retrieval of new updates from the upstream source. + #[serde(rename = "addr")] + upstream_addr: SocketAddr, + + /// The member ID used by the current local workspace. + /// This ID will be used to verify access permissions when connecting to the upstream server. + #[serde(rename = "as")] + using_account: MemberId, + + /// Whether the current member is interacting as a host. + /// In host mode, full Vault operation permissions are available except for adding new content. + #[serde(rename = "host")] + using_host_mode: bool, + + /// Whether the local workspace is stained. + /// + /// If stained, it can only set an upstream server with the same identifier. + /// + /// If the value is None, it means not stained; + /// otherwise, it contains the stain identifier (i.e., the upstream vault's unique ID) + #[serde(rename = "up_uid")] + stained_uuid: Option, + + /// The name of the sheet currently in use. + #[serde(rename = "use")] + sheet_in_use: Option, +} + +impl Default for LocalConfig { + fn default() -> Self { + Self { + upstream_addr: SocketAddr::V4(std::net::SocketAddrV4::new( + std::net::Ipv4Addr::new(127, 0, 0, 1), + PORT, + )), + using_account: "unknown".to_string(), + using_host_mode: false, + stained_uuid: None, + sheet_in_use: None, + } + } +} + +impl LocalConfig { + /// Set the vault address. + pub fn set_vault_addr(&mut self, addr: SocketAddr) { + self.upstream_addr = addr; + } + + /// Get the vault address. + pub fn vault_addr(&self) -> SocketAddr { + self.upstream_addr + } + + /// Set the currently used account + pub fn set_current_account(&mut self, account: MemberId) -> Result<(), std::io::Error> { + if self.sheet_in_use().is_some() { + return Err(Error::new( + std::io::ErrorKind::DirectoryNotEmpty, + "Please exit the current sheet before switching accounts", + )); + } + self.using_account = account; + Ok(()) + } + + /// Set the host mode + pub fn set_host_mode(&mut self, host_mode: bool) { + self.using_host_mode = host_mode; + } + + /// Set the currently used sheet + pub async fn use_sheet(&mut self, sheet: SheetName) -> Result<(), std::io::Error> { + let sheet = snake_case!(sheet); + + // Check if the sheet is already in use + if self.sheet_in_use().is_some() { + return Err(std::io::Error::new( + std::io::ErrorKind::AlreadyExists, + "Sheet already in use", + )); + }; + + // Check if the local path exists + let local_path = self.get_local_path().await?; + + // Get latest info + let Ok(latest_info) = LatestInfo::read_from(LatestInfo::latest_info_path( + &local_path, + &self.current_account(), + )) + .await + else { + return Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "No latest info found", + )); + }; + + // Check if the sheet exists + if !latest_info.visible_sheets.contains(&sheet) { + return Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "Sheet not found", + )); + } + + // Check if there are any files or folders other than .jv + self.check_local_path_empty(&local_path).await?; + + // Get the draft folder path + let draft_folder = self.draft_folder(&self.using_account, &sheet, &local_path); + + if draft_folder.exists() { + // Exists + // Move the contents of the draft folder to the local path with rollback support + self.move_draft_to_local(&draft_folder, &local_path).await?; + } + + self.sheet_in_use = Some(sheet); + LocalConfig::write(self).await?; + + Ok(()) + } + + /// Exit the currently used sheet + pub async fn exit_sheet(&mut self) -> Result<(), std::io::Error> { + // Check if the sheet is already in use + if self.sheet_in_use().is_none() { + return Ok(()); + } + + // Check if the local path exists + let local_path = self.get_local_path().await?; + + // Get the current sheet name + let sheet_name = self.sheet_in_use().as_ref().unwrap().clone(); + + // Get the draft folder path + let draft_folder = self.draft_folder(&self.using_account, &sheet_name, &local_path); + + // Create the draft folder if it doesn't exist + if !draft_folder.exists() { + std::fs::create_dir_all(&draft_folder).map_err(std::io::Error::other)?; + } + + // Move all files and folders (except .jv folder) to the draft folder with rollback support + self.move_local_to_draft(&local_path, &draft_folder).await?; + + // Clear the sheet in use + self.sheet_in_use = None; + LocalConfig::write(self).await?; + + Ok(()) + } + + /// Get local path or return error + async fn get_local_path(&self) -> Result { + current_local_path().ok_or_else(|| { + std::io::Error::new(std::io::ErrorKind::NotFound, "Fail to get local path") + }) + } + + /// Check if local path is empty (except for .jv folder) + async fn check_local_path_empty(&self, local_path: &Path) -> Result<(), std::io::Error> { + let jv_folder = local_path.join(CLIENT_PATH_WORKSPACE_ROOT); + let mut entries = std::fs::read_dir(local_path).map_err(std::io::Error::other)?; + + if entries.any(|entry| { + if let Ok(entry) = entry { + let path = entry.path(); + path != jv_folder + && path.file_name().and_then(|s| s.to_str()) + != Some(CLIENT_FOLDER_WORKSPACE_ROOT_NAME) + } else { + false + } + }) { + return Err(std::io::Error::new( + std::io::ErrorKind::DirectoryNotEmpty, + "Local path is not empty!", + )); + } + + Ok(()) + } + + /// Move contents from draft folder to local path with rollback support + async fn move_draft_to_local( + &self, + draft_folder: &Path, + local_path: &Path, + ) -> Result<(), std::io::Error> { + let draft_entries: Vec<_> = std::fs::read_dir(draft_folder) + .map_err(std::io::Error::other)? + .collect::, _>>() + .map_err(std::io::Error::other)?; + + let mut moved_items: Vec = Vec::new(); + + for entry in &draft_entries { + let entry_path = entry.path(); + let target_path = local_path.join(entry_path.file_name().unwrap()); + + // Move each file/directory from draft folder to local path + std::fs::rename(&entry_path, &target_path).map_err(|e| { + // Rollback all previously moved items + for moved_item in &moved_items { + let _ = std::fs::rename(&moved_item.target, &moved_item.source); + } + std::io::Error::other(e) + })?; + + moved_items.push(MovedItem { + source: entry_path.clone(), + target: target_path.clone(), + }); + } + + // Remove the now-empty draft folder + std::fs::remove_dir(draft_folder).map_err(|e| { + // Rollback all moved items if folder removal fails + for moved_item in &moved_items { + let _ = std::fs::rename(&moved_item.target, &moved_item.source); + } + std::io::Error::other(e) + })?; + + Ok(()) + } + + /// Move contents from local path to draft folder with rollback support (except .jv folder) + async fn move_local_to_draft( + &self, + local_path: &Path, + draft_folder: &Path, + ) -> Result<(), std::io::Error> { + let jv_folder = local_path.join(CLIENT_PATH_WORKSPACE_ROOT); + let entries: Vec<_> = std::fs::read_dir(local_path) + .map_err(std::io::Error::other)? + .collect::, _>>() + .map_err(std::io::Error::other)?; + + let mut moved_items: Vec = Vec::new(); + + for entry in &entries { + let entry_path = entry.path(); + + // Skip the .jv folder + if entry_path == jv_folder + || entry_path.file_name().and_then(|s| s.to_str()) + == Some(CLIENT_FOLDER_WORKSPACE_ROOT_NAME) + { + continue; + } + + let target_path = draft_folder.join(entry_path.file_name().unwrap()); + + // Move each file/directory from local path to draft folder + std::fs::rename(&entry_path, &target_path).map_err(|e| { + // Rollback all previously moved items + for moved_item in &moved_items { + let _ = std::fs::rename(&moved_item.target, &moved_item.source); + } + std::io::Error::other(e) + })?; + + moved_items.push(MovedItem { + source: entry_path.clone(), + target: target_path.clone(), + }); + } + + Ok(()) + } + + /// Get the currently used account + pub fn current_account(&self) -> MemberId { + self.using_account.clone() + } + + /// Check if the current member is interacting as a host. + pub fn is_host_mode(&self) -> bool { + self.using_host_mode + } + + /// Check if the local workspace is stained. + pub fn stained(&self) -> bool { + self.stained_uuid.is_some() + } + + /// Get the UUID of the vault that the local workspace is stained with. + pub fn stained_uuid(&self) -> Option { + self.stained_uuid + } + + /// Stain the local workspace with the given UUID. + pub fn stain(&mut self, uuid: VaultUuid) { + self.stained_uuid = Some(uuid); + } + + /// Unstain the local workspace. + pub fn unstain(&mut self) { + self.stained_uuid = None; + } + + /// Get the upstream address. + pub fn upstream_addr(&self) -> SocketAddr { + self.upstream_addr + } + + /// Get the currently used sheet + pub fn sheet_in_use(&self) -> &Option { + &self.sheet_in_use + } + + /// Get draft folder + pub fn draft_folder( + &self, + account: &MemberId, + sheet_name: &SheetName, + local_workspace_path: impl Into, + ) -> PathBuf { + let account_str = snake_case!(account.as_str()); + let sheet_name_str = snake_case!(sheet_name.as_str()); + let draft_path = CLIENT_PATH_LOCAL_DRAFT + .replace(KEY_ACCOUNT, &account_str) + .replace(KEY_SHEET_NAME, &sheet_name_str); + local_workspace_path.into().join(draft_path) + } + + /// Get current draft folder + pub fn current_draft_folder(&self) -> Option { + let Some(sheet_name) = self.sheet_in_use() else { + return None; + }; + + let current_dir = current_local_path()?; + + Some(self.draft_folder(&self.using_account, sheet_name, current_dir)) + } +} + +#[derive(Clone)] +struct MovedItem { + source: PathBuf, + target: PathBuf, +} diff --git a/legacy_data/src/data/member.rs b/legacy_data/src/data/member.rs new file mode 100644 index 0000000..7e99488 --- /dev/null +++ b/legacy_data/src/data/member.rs @@ -0,0 +1,71 @@ +use std::collections::HashMap; + +use cfg_file::ConfigFile; +use serde::{Deserialize, Serialize}; +use string_proc::snake_case; + +pub type MemberId = String; + +#[derive(Debug, Eq, Clone, ConfigFile, Serialize, Deserialize)] +pub struct Member { + /// Member ID, the unique identifier of the member + #[serde(rename = "id")] + id: String, + + /// Member metadata + #[serde(rename = "meta")] + metadata: HashMap, +} + +impl Default for Member { + fn default() -> Self { + Self::new("default_user") + } +} + +impl PartialEq for Member { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} + +impl std::fmt::Display for Member { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.id) + } +} + +impl std::convert::AsRef for Member { + fn as_ref(&self) -> &str { + &self.id + } +} + +impl Member { + /// Create member struct by id + pub fn new(new_id: impl Into) -> Self { + Self { + id: snake_case!(new_id.into()), + metadata: HashMap::new(), + } + } + + /// Get member id + pub fn id(&self) -> String { + self.id.clone() + } + + /// Get metadata + pub fn metadata(&self, key: impl Into) -> Option<&String> { + self.metadata.get(&key.into()) + } + + /// Set metadata + pub fn set_metadata( + &mut self, + key: impl AsRef, + value: impl Into, + ) -> Option { + self.metadata.insert(key.as_ref().to_string(), value.into()) + } +} diff --git a/legacy_data/src/data/sheet.rs b/legacy_data/src/data/sheet.rs new file mode 100644 index 0000000..8b427e9 --- /dev/null +++ b/legacy_data/src/data/sheet.rs @@ -0,0 +1,278 @@ +use std::{collections::HashMap, path::PathBuf}; + +use cfg_file::{ConfigFile, config::ConfigFile}; +use serde::{Deserialize, Serialize}; + +use crate::{ + constants::{KEY_SHEET_NAME, SERVER_FILE_SHEET}, + data::{ + member::MemberId, + vault::{ + Vault, + virtual_file::{VirtualFileId, VirtualFileVersion}, + }, + }, +}; + +pub type SheetName = String; +pub type SheetPathBuf = PathBuf; + +pub struct Sheet<'a> { + /// The name of the current sheet + pub(crate) name: SheetName, + + /// Sheet data + pub(crate) data: SheetData, + + /// Sheet path + pub(crate) vault_reference: &'a Vault, +} + +#[derive(Default, Serialize, Deserialize, ConfigFile, Clone)] +pub struct SheetData { + /// The write count of the current sheet + #[serde(rename = "v")] + pub(crate) write_count: i32, + + /// The holder of the current sheet, who has full operation rights to the sheet mapping + #[serde(rename = "holder")] + pub(crate) holder: Option, + + /// Mapping of sheet paths to virtual file IDs + #[serde(rename = "map")] + pub(crate) mapping: HashMap, + + /// Mapping of virtual file Ids to sheet paths + #[serde(rename = "id_map")] + pub(crate) id_mapping: Option>, +} + +#[derive(Debug, Default, Serialize, Deserialize, ConfigFile, Clone, Eq, PartialEq)] +pub struct SheetMappingMetadata { + #[serde(rename = "id")] + pub id: VirtualFileId, + #[serde(rename = "ver")] + pub version: VirtualFileVersion, +} + +impl<'a> Sheet<'a> { + pub fn name(&self) -> &SheetName { + &self.name + } + + /// Get the holder of this sheet + pub fn holder(&self) -> Option<&MemberId> { + self.data.holder.as_ref() + } + + /// Get the mapping of this sheet + pub fn mapping(&self) -> &HashMap { + &self.data.mapping + } + + /// Get the muttable mapping of this sheet + pub fn mapping_mut(&mut self) -> &mut HashMap { + &mut self.data.mapping + } + + /// Get the id_mapping of this sheet data + pub fn id_mapping(&self) -> &Option> { + &self.data.id_mapping + } + + /// Get the write count of this sheet + pub fn write_count(&self) -> i32 { + self.data.write_count + } + + /// Forget the holder of this sheet + pub fn forget_holder(&mut self) { + self.data.holder = None; + } + + /// Set the holder of this sheet + pub fn set_holder(&mut self, holder: MemberId) { + self.data.holder = Some(holder); + } + + /// Add (or Edit) a mapping entry to the sheet + /// + /// This operation performs safety checks to ensure the member has the right to add the mapping: + /// 1. The sheet must have a holder (member) to perform this operation + /// 2. If the virtual file ID doesn't exist in the vault, the mapping is added directly + /// 3. If the virtual file exists, the mapping is added regardless of member edit rights + /// + /// Note: Full validation adds overhead - avoid frequent calls + pub async fn add_mapping( + &mut self, + sheet_path: SheetPathBuf, + virtual_file_id: VirtualFileId, + version: VirtualFileVersion, + ) -> Result<(), std::io::Error> { + // Check if the virtual file exists in the vault + if self.vault_reference.virtual_file(&virtual_file_id).is_err() { + // Virtual file doesn't exist, add the mapping directly + self.data.mapping.insert( + sheet_path, + SheetMappingMetadata { + id: virtual_file_id, + version, + }, + ); + return Ok(()); + } + + // Check if the sheet has a holder + let Some(_) = self.holder() else { + return Err(std::io::Error::new( + std::io::ErrorKind::PermissionDenied, + "This sheet has no holder", + )); + }; + + self.data.mapping.insert( + sheet_path, + SheetMappingMetadata { + id: virtual_file_id, + version, + }, + ); + + Ok(()) + } + + /// Remove a mapping entry from the sheet + /// + /// This operation performs safety checks to ensure the member has the right to remove the mapping: + /// 1. The sheet must have a holder (member) to perform this operation + /// 2. Member must NOT have edit rights to the virtual file to release it (ensuring clear ownership) + /// 3. If the virtual file doesn't exist, the mapping is removed but no ID is returned + /// 4. If member has no edit rights and the file exists, returns the removed virtual file ID + /// + /// Note: Full validation adds overhead - avoid frequent calls + pub async fn remove_mapping( + &mut self, + sheet_path: &SheetPathBuf, + ) -> Option { + let virtual_file_meta = match self.data.mapping.get(sheet_path) { + Some(id) => id, + None => { + // The mapping entry doesn't exist, nothing to remove + return None; + } + }; + + // Check if the virtual file exists in the vault + if self + .vault_reference + .virtual_file(&virtual_file_meta.id) + .is_err() + { + // Virtual file doesn't exist, remove the mapping and return None + self.data.mapping.remove(sheet_path); + return None; + } + + // Check if the sheet has a holder + let holder = self.holder()?; + + // Check if the holder has edit rights to the virtual file + match self + .vault_reference + .has_virtual_file_edit_right(holder, &virtual_file_meta.id) + .await + { + Ok(false) => { + // Holder doesn't have rights, remove and return the virtual file ID + self.data.mapping.remove(sheet_path) + } + Ok(true) => { + // Holder has edit rights, don't remove the mapping + None + } + Err(_) => { + // Error checking rights, don't remove the mapping + None + } + } + } + + /// Persist the sheet to disk + /// + /// Why not use a reference? + /// Because I don't want a second instance of the sheet to be kept in memory. + /// If needed, please deserialize and reload it. + pub async fn persist(mut self) -> Result<(), std::io::Error> { + self.data.write_count += 1; + + // Update id mapping + self.data.id_mapping = Some(HashMap::new()); + for map in self.data.mapping.iter() { + self.data + .id_mapping + .as_mut() + .unwrap() + .insert(map.1.id.clone(), map.0.clone()); + } + + // Add write count + if self.data.write_count >= i32::MAX - 1 { + self.data.write_count = 0; + } + SheetData::write_to(&self.data, self.sheet_path()).await + } + + /// Get the path to the sheet file + pub fn sheet_path(&self) -> PathBuf { + Sheet::sheet_path_with_name(self.vault_reference, &self.name) + } + + /// Get the path to the sheet file with the given name + pub fn sheet_path_with_name(vault: &Vault, name: impl AsRef) -> PathBuf { + vault + .vault_path() + .join(SERVER_FILE_SHEET.replace(KEY_SHEET_NAME, name.as_ref())) + } + + /// Clone the data of the sheet + pub fn clone_data(&self) -> SheetData { + self.data.clone() + } + + /// Convert the sheet into its data representation + pub fn to_data(self) -> SheetData { + self.data + } +} + +impl SheetData { + /// Get the write count of this sheet data + pub fn write_count(&self) -> i32 { + self.write_count + } + + /// Get the holder of this sheet data + pub fn holder(&self) -> Option<&MemberId> { + self.holder.as_ref() + } + + /// Get the mapping of this sheet data + pub fn mapping(&self) -> &HashMap { + &self.mapping + } + + /// Get the muttable mapping of this sheet data + pub fn mapping_mut(&mut self) -> &mut HashMap { + &mut self.mapping + } + + /// Get the id_mapping of this sheet data + pub fn id_mapping(&self) -> &Option> { + &self.id_mapping + } + + /// Get the muttable id_mapping of this sheet data + pub fn id_mapping_mut(&mut self) -> &mut Option> { + &mut self.id_mapping + } +} diff --git a/legacy_data/src/data/user.rs b/legacy_data/src/data/user.rs new file mode 100644 index 0000000..a2326fa --- /dev/null +++ b/legacy_data/src/data/user.rs @@ -0,0 +1,28 @@ +use crate::env::current_cfg_dir; +use std::path::PathBuf; + +pub mod accounts; + +pub struct UserDirectory { + local_path: PathBuf, +} + +impl UserDirectory { + /// Create a user ditectory struct from the current system's document directory + pub fn current_cfg_dir() -> Option { + Some(UserDirectory { + local_path: current_cfg_dir()?, + }) + } + + /// Create a user directory struct from a specified directory path + /// Returns None if the directory does not exist + pub fn from_path>(path: P) -> Option { + let local_path = path.into(); + if local_path.exists() { + Some(UserDirectory { local_path }) + } else { + None + } + } +} diff --git a/legacy_data/src/data/user/accounts.rs b/legacy_data/src/data/user/accounts.rs new file mode 100644 index 0000000..def2677 --- /dev/null +++ b/legacy_data/src/data/user/accounts.rs @@ -0,0 +1,162 @@ +use std::{ + fs, + io::{Error, ErrorKind}, + path::PathBuf, +}; + +use cfg_file::config::ConfigFile; + +use crate::{ + constants::{KEY_SELF_ID, USER_FILE_ACCOUNTS, USER_FILE_KEY, USER_FILE_MEMBER}, + data::{ + member::{Member, MemberId}, + user::UserDirectory, + }, +}; + +/// Account Management +impl UserDirectory { + /// Read account from configuration file + pub async fn account(&self, id: &MemberId) -> Result { + if let Some(cfg_file) = self.account_cfg(id) { + let member = Member::read_from(cfg_file).await?; + return Ok(member); + } + + Err(Error::new(ErrorKind::NotFound, "Account not found!")) + } + + /// List all account IDs in the user directory + pub fn account_ids(&self) -> Result, std::io::Error> { + let accounts_path = self + .local_path + .join(USER_FILE_ACCOUNTS.replace(KEY_SELF_ID, "")); + + if !accounts_path.exists() { + return Ok(Vec::new()); + } + + let mut account_ids = Vec::new(); + + for entry in fs::read_dir(accounts_path)? { + let entry = entry?; + let path = entry.path(); + + if path.is_file() + && let Some(file_name) = path.file_stem().and_then(|s| s.to_str()) + && path.extension().and_then(|s| s.to_str()) == Some("toml") + { + // Remove the "_private" suffix from key files if present + let account_id = file_name.replace("_private", ""); + account_ids.push(account_id); + } + } + + Ok(account_ids) + } + + /// Get all accounts + /// This method will read and deserialize account information, please pay attention to performance issues + pub async fn accounts(&self) -> Result, std::io::Error> { + let mut accounts = Vec::new(); + + for account_id in self.account_ids()? { + if let Ok(account) = self.account(&account_id).await { + accounts.push(account); + } + } + + Ok(accounts) + } + + /// Update account info + pub async fn update_account(&self, member: Member) -> Result<(), std::io::Error> { + // Ensure account exist + if self.account_cfg(&member.id()).is_some() { + let account_cfg_path = self.account_cfg_path(&member.id()); + Member::write_to(&member, account_cfg_path).await?; + return Ok(()); + } + + Err(Error::new(ErrorKind::NotFound, "Account not found!")) + } + + /// Register an account to user directory + pub async fn register_account(&self, member: Member) -> Result<(), std::io::Error> { + // Ensure account not exist + if self.account_cfg(&member.id()).is_some() { + return Err(Error::new( + ErrorKind::DirectoryNotEmpty, + format!("Account `{}` already registered!", member.id()), + )); + } + + // Ensure accounts directory exists + let accounts_dir = self + .local_path + .join(USER_FILE_ACCOUNTS.replace(KEY_SELF_ID, "")); + if !accounts_dir.exists() { + fs::create_dir_all(&accounts_dir)?; + } + + // Write config file to accounts dir + let account_cfg_path = self.account_cfg_path(&member.id()); + Member::write_to(&member, account_cfg_path).await?; + + Ok(()) + } + + /// Remove account from user directory + pub fn remove_account(&self, id: &MemberId) -> Result<(), std::io::Error> { + // Remove config file if exists + if let Some(account_cfg_path) = self.account_cfg(id) { + fs::remove_file(account_cfg_path)?; + } + + // Remove private key file if exists + if let Some(private_key_path) = self.account_private_key(id) + && private_key_path.exists() + { + fs::remove_file(private_key_path)?; + } + + Ok(()) + } + + /// Try to get the account's configuration file to determine if the account exists + pub fn account_cfg(&self, id: &MemberId) -> Option { + let cfg_file = self.account_cfg_path(id); + if cfg_file.exists() { + Some(cfg_file) + } else { + None + } + } + + /// Try to get the account's private key file to determine if the account has a private key + pub fn account_private_key(&self, id: &MemberId) -> Option { + let key_file = self.account_private_key_path(id); + if key_file.exists() { + Some(key_file) + } else { + None + } + } + + /// Check if account has private key + pub fn has_private_key(&self, id: &MemberId) -> bool { + self.account_private_key(id).is_some() + } + + /// Get the account's configuration file path, but do not check if the file exists + pub fn account_cfg_path(&self, id: &MemberId) -> PathBuf { + self.local_path + .join(USER_FILE_MEMBER.replace(KEY_SELF_ID, id.to_string().as_str())) + } + + /// Get the account's private key file path, but do not check if the file exists + pub fn account_private_key_path(&self, id: &MemberId) -> PathBuf { + self.local_path + .join(USER_FILE_KEY.replace(KEY_SELF_ID, id.to_string().as_str())) + } +} diff --git a/legacy_data/src/data/vault.rs b/legacy_data/src/data/vault.rs new file mode 100644 index 0000000..0f93016 --- /dev/null +++ b/legacy_data/src/data/vault.rs @@ -0,0 +1,132 @@ +use std::{env::current_dir, path::PathBuf, sync::Arc}; + +use tokio::fs::create_dir_all; +use vcs_docs::docs::READMES_VAULT_README; + +use crate::{ + constants::{ + REF_SHEET_NAME, SERVER_FILE_README, SERVER_FILE_VAULT, SERVER_PATH_MEMBER_PUB, + SERVER_PATH_MEMBERS, SERVER_PATH_SHEETS, SERVER_PATH_VF_ROOT, VAULT_HOST_NAME, + }, + data::{member::Member, vault::vault_config::VaultConfig}, + env::{current_vault_path, find_vault_path}, +}; + +pub mod lock_status; +pub mod mapping_share; +pub mod member_manage; +pub mod sheet_manage; +pub mod vault_config; +pub mod virtual_file; + +pub struct Vault { + config: Arc, + vault_path: PathBuf, +} + +impl Vault { + /// Get vault path + pub fn vault_path(&self) -> &PathBuf { + &self.vault_path + } + + /// Initialize vault + pub fn init(config: VaultConfig, vault_path: impl Into) -> Option { + let vault_path = find_vault_path(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: Arc::new(config), + vault_path, + }) + } + + /// Setup vault + pub async fn setup_vault( + vault_path: impl Into, + vault_name: impl AsRef, + ) -> Result<(), std::io::Error> { + let vault_path: PathBuf = vault_path.into(); + + // Ensure directory is empty + if vault_path.exists() && vault_path.read_dir()?.next().is_some() { + return Err(std::io::Error::new( + std::io::ErrorKind::DirectoryNotEmpty, + "DirectoryNotEmpty", + )); + } + + // 1. Setup main config + let config = VaultConfig::default(); + + // NOTE: + // Do not use the write_to method provided by the ConfigFile trait to store the Vault configuration file + // Instead, use the PROFILES_VAULT content provided by the Documents Repository for writing + + // VaultConfig::write_to(&config, vault_path.join(SERVER_FILE_VAULT)).await?; + let config_content = vcs_docs::docs::PROFILES_VAULT + .replace("{vault_name}", vault_name.as_ref()) + .replace("{user_name}", whoami::username().as_str()) + .replace( + "{date_format}", + chrono::Local::now() + .format("%Y-%m-%d %H:%M") + .to_string() + .as_str(), + ) + .replace("{vault_uuid}", &config.vault_uuid().to_string()); + tokio::fs::write(vault_path.join(SERVER_FILE_VAULT), config_content).await?; + + // 2. Setup sheets directory + create_dir_all(vault_path.join(SERVER_PATH_SHEETS)).await?; + + // 3. Setup key directory + create_dir_all(vault_path.join(SERVER_PATH_MEMBER_PUB)).await?; + + // 4. Setup member directory + create_dir_all(vault_path.join(SERVER_PATH_MEMBERS)).await?; + + // 5. Setup storage directory + create_dir_all(vault_path.join(SERVER_PATH_VF_ROOT)).await?; + + let Some(vault) = Vault::init(config, &vault_path) else { + return Err(std::io::Error::other("Failed to initialize vault")); + }; + + // 6. Create host member + vault + .register_member_to_vault(Member::new(VAULT_HOST_NAME)) + .await?; + + // 7. Setup reference sheet + vault + .create_sheet(&REF_SHEET_NAME.to_string(), &VAULT_HOST_NAME.to_string()) + .await?; + + // Final, generate README.md + let readme_content = READMES_VAULT_README; + tokio::fs::write(vault_path.join(SERVER_FILE_README), readme_content).await?; + + Ok(()) + } + + /// Setup vault in current directory + pub async fn setup_vault_current_dir( + vault_name: impl AsRef, + ) -> Result<(), std::io::Error> { + Self::setup_vault(current_dir()?, vault_name).await?; + Ok(()) + } + + /// Get vault configuration + pub fn config(&self) -> &Arc { + &self.config + } +} 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(()) + } +} diff --git a/legacy_data/src/data/vault/mapping_share.rs b/legacy_data/src/data/vault/mapping_share.rs new file mode 100644 index 0000000..5d27859 --- /dev/null +++ b/legacy_data/src/data/vault/mapping_share.rs @@ -0,0 +1,422 @@ +use std::{collections::HashMap, io::Error, path::PathBuf}; + +use cfg_file::{ConfigFile, config::ConfigFile}; +use rand::{Rng, rng}; +use serde::{Deserialize, Serialize}; +use string_proc::{format_path, snake_case}; +use tokio::fs; + +use crate::{ + constants::{ + KEY_SHARE_ID, KEY_SHEET_NAME, SERVER_FILE_SHEET_SHARE, SERVER_PATH_SHARES, + SERVER_SUFFIX_SHEET_SHARE_FILE_NO_DOT, + }, + data::{ + member::MemberId, + sheet::{Sheet, SheetMappingMetadata, SheetName, SheetPathBuf}, + vault::Vault, + }, +}; + +pub type SheetShareId = String; + +#[derive(Default, Serialize, Deserialize, ConfigFile, Clone, Debug)] +pub struct Share { + /// Sharer: the member who created this share item + #[serde(rename = "sharer")] + pub sharer: MemberId, + + /// Description of the share item + #[serde(rename = "desc")] + pub description: String, + + /// Metadata path + #[serde(skip)] + pub path: Option, + + /// From: which sheet the member exported the file from + #[serde(rename = "from")] + pub from_sheet: SheetName, + + /// Mappings: the sheet mappings contained in the share item + #[serde(rename = "map")] + pub mappings: HashMap, +} + +#[derive(Default, Serialize, Deserialize, ConfigFile, Clone, PartialEq, Eq)] +pub enum ShareMergeMode { + /// If a path or file already exists during merge, prioritize the incoming share + /// Path conflict: replace the mapping content at the local path with the incoming content + /// File conflict: delete the original file mapping and create a new one + Overwrite, + + /// If a path or file already exists during merge, skip overwriting this entry + Skip, + + /// Pre-check for conflicts, prohibit merging if any conflicts are found + #[default] + Safe, + + /// Reject all shares + RejectAll, +} + +#[derive(Default, Serialize, Deserialize, ConfigFile, Clone)] +pub struct ShareMergeConflict { + /// Duplicate mappings exist + pub duplicate_mapping: Vec, + + /// Duplicate files exist + pub duplicate_file: Vec, +} + +impl ShareMergeConflict { + /// Check if there are no conflicts + pub fn ok(&self) -> bool { + self.duplicate_mapping.is_empty() && self.duplicate_file.is_empty() + } +} + +impl Vault { + /// Get the path of a share item in a sheet + pub fn share_file_path(&self, sheet_name: &SheetName, share_id: &SheetShareId) -> PathBuf { + let sheet_name = snake_case!(sheet_name.clone()); + let share_id = share_id.clone(); + + // Format the path to remove "./" prefix and normalize it + let path_str = SERVER_FILE_SHEET_SHARE + .replace(KEY_SHEET_NAME, &sheet_name) + .replace(KEY_SHARE_ID, &share_id); + + // Use format_path to normalize the path + match format_path::format_path_str(&path_str) { + Ok(normalized_path) => self.vault_path().join(normalized_path), + Err(_) => { + // Fallback to original behavior if formatting fails + self.vault_path().join(path_str) + } + } + } + + /// Get the actual paths of all share items in a sheet + pub async fn share_file_paths(&self, sheet_name: &SheetName) -> Vec { + let sheet_name = snake_case!(sheet_name.clone()); + let shares_dir = self + .vault_path() + .join(SERVER_PATH_SHARES.replace(KEY_SHEET_NAME, &sheet_name)); + + let mut result = Vec::new(); + if let Ok(mut entries) = fs::read_dir(shares_dir).await { + while let Ok(Some(entry)) = entries.next_entry().await { + let path = entry.path(); + if path.is_file() + && path.extension().and_then(|s| s.to_str()) + == Some(SERVER_SUFFIX_SHEET_SHARE_FILE_NO_DOT) + { + result.push(path); + } + } + } + result + } +} + +impl<'a> Sheet<'a> { + /// Get the shares of a sheet + pub async fn get_shares(&self) -> Result, std::io::Error> { + let paths = self.vault_reference.share_file_paths(&self.name).await; + let mut shares = Vec::new(); + + for path in paths { + match Share::read_from(&path).await { + Ok(mut share) => { + share.path = Some(path); + shares.push(share); + } + Err(e) => return Err(e), + } + } + + Ok(shares) + } + + /// Get a share of a sheet + pub async fn get_share(&self, share_id: &SheetShareId) -> Result { + let path = self.vault_reference.share_file_path(&self.name, share_id); + let mut share = Share::read_from(&path).await?; + share.path = Some(path); + Ok(share) + } + + /// Import a share of a sheet by its ID + pub async fn merge_share_by_id( + self, + share_id: &SheetShareId, + share_merge_mode: ShareMergeMode, + ) -> Result<(), std::io::Error> { + let share = self.get_share(share_id).await?; + self.merge_share(share, share_merge_mode).await + } + + /// Import a share of a sheet + pub async fn merge_share( + mut self, + share: Share, + share_merge_mode: ShareMergeMode, + ) -> Result<(), std::io::Error> { + // Backup original data and edit based on this backup + let mut copy_share = share.clone(); + let mut copy_sheet = self.clone_data(); + + // Pre-check + let conflicts = self.precheck(©_share); + let mut reject_mode = false; + + match share_merge_mode { + // Safe mode: conflicts are not allowed + ShareMergeMode::Safe => { + // Conflicts found + if !conflicts.ok() { + // Do nothing, return Error + return Err(Error::new( + std::io::ErrorKind::AlreadyExists, + "Mappings or files already exist!", + )); + } + } + // Overwrite mode: when conflicts occur, prioritize the share item + ShareMergeMode::Overwrite => { + // Handle duplicate mappings + for path in conflicts.duplicate_mapping { + // Get the share data + let Some(share_value) = copy_share.mappings.remove(&path) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("Share value `{}` not found!", &path.display()), + )); + }; + // Overwrite + copy_sheet.mapping_mut().insert(path, share_value); + } + + // Handle duplicate IDs + for path in conflicts.duplicate_file { + // Get the share data + let Some(share_value) = copy_share.mappings.remove(&path) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("Share value `{}` not found!", &path.display()), + )); + }; + + // Extract the file ID + let conflict_vfid = &share_value.id; + + // Through the sheet's ID mapping + let Some(id_mapping) = copy_sheet.id_mapping_mut() else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Id mapping not found!", + )); + }; + + // Get the original path from the ID mapping + let Some(raw_path) = id_mapping.remove(conflict_vfid) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("The path of virtual file `{}' not found!", conflict_vfid), + )); + }; + + // Remove the original path mapping + if copy_sheet.mapping_mut().remove(&raw_path).is_none() { + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("Remove mapping `{}` failed!", &raw_path.display()), + )); + } + // Insert the new item + copy_sheet.mapping_mut().insert(path, share_value); + } + } + // Skip mode: when conflicts occur, prioritize the local sheet + ShareMergeMode::Skip => { + // Directly remove conflicting items + for path in conflicts.duplicate_mapping { + copy_share.mappings.remove(&path); + } + for path in conflicts.duplicate_file { + copy_share.mappings.remove(&path); + } + } + // Reject all mode: reject all shares + ShareMergeMode::RejectAll => { + reject_mode = true; // Only mark as rejected + } + } + + if !reject_mode { + // Subsequent merging + copy_sheet + .mapping_mut() + .extend(copy_share.mappings.into_iter()); + + // Merge completed + self.data = copy_sheet; // Write the result + + // Merge completed, consume the sheet + self.persist().await.map_err(|err| { + Error::new( + std::io::ErrorKind::NotFound, + format!("Write sheet failed: {}", err), + ) + })?; + } + + // Persistence succeeded, continue to consume the share item + share.remove().await.map_err(|err| { + Error::new( + std::io::ErrorKind::NotFound, + format!("Remove share failed: {}", err.1), + ) + }) + } + + // Pre-check whether the share can be imported into the current sheet without conflicts + fn precheck(&self, share: &Share) -> ShareMergeConflict { + let mut conflicts = ShareMergeConflict::default(); + + for (mapping, metadata) in &share.mappings { + // Check for duplicate mappings + if self.mapping().contains_key(mapping.as_path()) { + conflicts.duplicate_mapping.push(mapping.clone()); + continue; + } + + // Check for duplicate IDs + if let Some(id_mapping) = self.id_mapping() { + if id_mapping.contains_key(&metadata.id) { + conflicts.duplicate_file.push(mapping.clone()); + continue; + } + } + } + + conflicts + } + + /// Share mappings with another sheet + pub async fn share_mappings( + &self, + other_sheet: &SheetName, + mappings: Vec, + sharer: &MemberId, + description: String, + ) -> Result { + let other_sheet = snake_case!(other_sheet.clone()); + let sharer = snake_case!(sharer.clone()); + + // Check if the sheet exists + let sheet_names = self.vault_reference.sheet_names()?; + if !sheet_names.contains(&other_sheet) { + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("Sheet `{}` not found!", &other_sheet), + )); + } + + // Check if the target file exists, regenerate ID if path already exists, up to 20 attempts + let target_path = { + let mut id; + let mut share_path; + let mut attempts = 0; + + loop { + id = Share::gen_share_id(&sharer); + share_path = self.vault_reference.share_file_path(&other_sheet, &id); + + if !share_path.exists() { + break share_path; + } + + attempts += 1; + if attempts >= 20 { + return Err(Error::new( + std::io::ErrorKind::AlreadyExists, + "Failed to generate unique share ID after 20 attempts!", + )); + } + } + }; + + // Validate that the share is valid + let mut share_mappings = HashMap::new(); + for mapping_path in &mappings { + if let Some(metadata) = self.mapping().get(mapping_path) { + share_mappings.insert(mapping_path.clone(), metadata.clone()); + } else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("Mapping `{}` not found in sheet!", mapping_path.display()), + )); + } + } + + // Build share data + let share_data = Share { + sharer, + description, + path: None, // This is only needed during merging (reading), no need to serialize now + from_sheet: self.name.clone(), + mappings: share_mappings, + }; + + // Write data + Share::write_to(&share_data, target_path).await?; + + Ok(share_data) + } +} + +impl Share { + /// Generate a share ID for a given sharer + pub fn gen_share_id(sharer: &MemberId) -> String { + let sharer_snake = snake_case!(sharer.clone()); + let random_part: String = rng() + .sample_iter(&rand::distr::Alphanumeric) + .take(8) + .map(char::from) + .collect(); + format!("{}@{}", sharer_snake, random_part) + } + + /// Delete a share (reject or remove the share item) + /// If deletion succeeds, returns `Ok(())`; + /// If deletion fails, returns `Err((self, std::io::Error))`, containing the original share object and the error information. + pub async fn remove(self) -> Result<(), (Self, std::io::Error)> { + let Some(path) = &self.path else { + return Err(( + self, + Error::new(std::io::ErrorKind::NotFound, "No share path recorded!"), + )); + }; + + if !path.exists() { + return Err(( + self, + Error::new(std::io::ErrorKind::NotFound, "No share file exists!"), + )); + } + + match fs::remove_file(path).await { + Err(err) => Err(( + self, + Error::new( + std::io::ErrorKind::Other, + format!("Failed to delete share file: {}", err), + ), + )), + Ok(_) => Ok(()), + } + } +} diff --git a/legacy_data/src/data/vault/member_manage.rs b/legacy_data/src/data/vault/member_manage.rs new file mode 100644 index 0000000..9d22d09 --- /dev/null +++ b/legacy_data/src/data/vault/member_manage.rs @@ -0,0 +1,144 @@ +use std::{ + fs, + io::{Error, ErrorKind}, + path::PathBuf, +}; + +use cfg_file::config::ConfigFile; + +use crate::{ + constants::{ + SERVER_FILE_MEMBER_INFO, SERVER_FILE_MEMBER_PUB, SERVER_PATH_MEMBERS, + SERVER_SUFFIX_MEMBER_INFO_NO_DOT, + }, + data::{ + member::{Member, MemberId}, + vault::Vault, + }, +}; + +const ID_PARAM: &str = "{member_id}"; + +/// Member Manage +impl Vault { + /// Read member from configuration file + pub async fn member(&self, id: &MemberId) -> Result { + if let Some(cfg_file) = self.member_cfg(id) { + let member = Member::read_from(cfg_file).await?; + return Ok(member); + } + + Err(Error::new(ErrorKind::NotFound, "Member not found!")) + } + + /// List all member IDs in the vault + pub fn member_ids(&self) -> Result, std::io::Error> { + let members_path = self.vault_path.join(SERVER_PATH_MEMBERS); + + if !members_path.exists() { + return Ok(Vec::new()); + } + + let mut member_ids = Vec::new(); + + for entry in fs::read_dir(members_path)? { + let entry = entry?; + let path = entry.path(); + + if path.is_file() + && let Some(file_name) = path.file_stem().and_then(|s| s.to_str()) + && path.extension().and_then(|s| s.to_str()) + == Some(SERVER_SUFFIX_MEMBER_INFO_NO_DOT) + { + member_ids.push(file_name.to_string()); + } + } + + Ok(member_ids) + } + + /// Get all members + /// This method will read and deserialize member information, please pay attention to performance issues + pub async fn members(&self) -> Result, std::io::Error> { + let mut members = Vec::new(); + + for member_id in self.member_ids()? { + if let Ok(member) = self.member(&member_id).await { + members.push(member); + } + } + + Ok(members) + } + + /// Update member info + pub async fn update_member(&self, member: Member) -> Result<(), std::io::Error> { + // Ensure member exist + if self.member_cfg(&member.id()).is_some() { + let member_cfg_path = self.member_cfg_path(&member.id()); + Member::write_to(&member, member_cfg_path).await?; + return Ok(()); + } + + Err(Error::new(ErrorKind::NotFound, "Member not found!")) + } + + /// Register a member to vault + pub async fn register_member_to_vault(&self, member: Member) -> Result<(), std::io::Error> { + // Ensure member not exist + if self.member_cfg(&member.id()).is_some() { + return Err(Error::new( + ErrorKind::DirectoryNotEmpty, + format!("Member `{}` already registered!", member.id()), + )); + } + + // Wrtie config file to member dir + let member_cfg_path = self.member_cfg_path(&member.id()); + Member::write_to(&member, member_cfg_path).await?; + + Ok(()) + } + + /// Remove member from vault + pub fn remove_member_from_vault(&self, id: &MemberId) -> Result<(), std::io::Error> { + // Ensure member exist + if let Some(member_cfg_path) = self.member_cfg(id) { + fs::remove_file(member_cfg_path)?; + } + + Ok(()) + } + + /// Try to get the member's configuration file to determine if the member exists + pub fn member_cfg(&self, id: &MemberId) -> Option { + let cfg_file = self.member_cfg_path(id); + if cfg_file.exists() { + Some(cfg_file) + } else { + None + } + } + + /// Try to get the member's public key file to determine if the member has login permission + pub fn member_key(&self, id: &MemberId) -> Option { + let key_file = self.member_key_path(id); + if key_file.exists() { + Some(key_file) + } else { + None + } + } + + /// Get the member's configuration file path, but do not check if the file exists + pub fn member_cfg_path(&self, id: &MemberId) -> PathBuf { + self.vault_path + .join(SERVER_FILE_MEMBER_INFO.replace(ID_PARAM, id.to_string().as_str())) + } + + /// Get the member's public key file path, but do not check if the file exists + pub fn member_key_path(&self, id: &MemberId) -> PathBuf { + self.vault_path + .join(SERVER_FILE_MEMBER_PUB.replace(ID_PARAM, id.to_string().as_str())) + } +} diff --git a/legacy_data/src/data/vault/sheet_manage.rs b/legacy_data/src/data/vault/sheet_manage.rs new file mode 100644 index 0000000..c22c849 --- /dev/null +++ b/legacy_data/src/data/vault/sheet_manage.rs @@ -0,0 +1,274 @@ +use std::{collections::HashMap, io::Error}; + +use cfg_file::config::ConfigFile; +use string_proc::snake_case; +use tokio::fs; + +use crate::{ + constants::{SERVER_PATH_SHEETS, SERVER_SUFFIX_SHEET_FILE_NO_DOT}, + data::{ + member::MemberId, + sheet::{Sheet, SheetData, SheetName}, + vault::Vault, + }, +}; + +/// Vault Sheets Management +impl Vault { + /// Load all sheets in the vault + /// + /// It is generally not recommended to call this function frequently. + /// Although a vault typically won't contain too many sheets, + /// if individual sheet contents are large, this operation may cause + /// significant performance bottlenecks. + pub async fn sheets<'a>(&'a self) -> Result>, std::io::Error> { + let sheet_names = self.sheet_names()?; + let mut sheets = Vec::new(); + + for sheet_name in sheet_names { + let sheet = self.sheet(&sheet_name).await?; + sheets.push(sheet); + } + + Ok(sheets) + } + + /// Search for all sheet names in the vault + /// + /// The complexity of this operation is proportional to the number of sheets, + /// but generally there won't be too many sheets in a Vault + pub fn sheet_names(&self) -> Result, std::io::Error> { + // Get the sheets directory path + let sheets_dir = self.vault_path.join(SERVER_PATH_SHEETS); + + // If the directory doesn't exist, return an empty list + if !sheets_dir.exists() { + return Ok(vec![]); + } + + let mut sheet_names = Vec::new(); + + // Iterate through all files in the sheets directory + for entry in std::fs::read_dir(sheets_dir)? { + let entry = entry?; + let path = entry.path(); + + // Check if it's a YAML file + if path.is_file() + && path + .extension() + .is_some_and(|ext| ext == SERVER_SUFFIX_SHEET_FILE_NO_DOT) + && let Some(file_stem) = path.file_stem().and_then(|s| s.to_str()) + { + // Create a new SheetName and add it to the result list + sheet_names.push(file_stem.to_string()); + } + } + + Ok(sheet_names) + } + + /// Read a sheet from its name + /// + /// If the sheet information is successfully found in the vault, + /// it will be deserialized and read as a sheet. + /// This is the only correct way to obtain a sheet instance. + pub async fn sheet<'a>(&'a self, sheet_name: &SheetName) -> Result, std::io::Error> { + let sheet_name = snake_case!(sheet_name.clone()); + + // Get the path to the sheet file + let sheet_path = Sheet::sheet_path_with_name(self, &sheet_name); + + // Ensure the sheet file exists + if !sheet_path.exists() { + // If the sheet does not exist, try to restore it from the trash + if self.restore_sheet(&sheet_name).await.is_err() { + // If restoration fails, return an error + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("Sheet `{}` not found!", sheet_name), + )); + } + } + + // Read the sheet data from the file + let data = SheetData::read_from(sheet_path).await?; + + Ok(Sheet { + name: sheet_name.clone(), + data, + vault_reference: self, + }) + } + + /// Create a sheet locally and return the sheet instance + /// + /// This method creates a new sheet in the vault with the given name and holder. + /// It will verify that the member exists and that the sheet doesn't already exist + /// before creating the sheet file with default empty data. + pub async fn create_sheet<'a>( + &'a self, + sheet_name: &SheetName, + holder: &MemberId, + ) -> Result, std::io::Error> { + let sheet_name = snake_case!(sheet_name.clone()); + + // Ensure member exists + if !self.member_cfg_path(holder).exists() { + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("Member `{}` not found!", &holder), + )); + } + + // Ensure sheet does not already exist + let sheet_file_path = Sheet::sheet_path_with_name(self, &sheet_name); + if sheet_file_path.exists() { + return Err(Error::new( + std::io::ErrorKind::AlreadyExists, + format!("Sheet `{}` already exists!", &sheet_name), + )); + } + + // Create the sheet file + let sheet_data = SheetData { + holder: Some(holder.clone()), + mapping: HashMap::new(), + id_mapping: None, + write_count: 0, + }; + SheetData::write_to(&sheet_data, sheet_file_path).await?; + + Ok(Sheet { + name: sheet_name, + data: sheet_data, + vault_reference: self, + }) + } + + /// Delete the sheet file from local disk by name + /// + /// This method will remove the sheet file with the given name from the vault. + /// It will verify that the sheet exists before attempting to delete it. + /// If the sheet is successfully deleted, it will return Ok(()). + /// + /// Warning: This operation is dangerous. Deleting a sheet will cause local workspaces + /// using this sheet to become invalid. Please ensure the sheet is not currently in use + /// and will not be used in the future. + /// + /// For a safer deletion method, consider using `delete_sheet_safety`. + /// + /// Note: This function is intended for server-side use only and should not be + /// arbitrarily called by other members to prevent unauthorized data deletion. + pub async fn delete_sheet(&self, sheet_name: &SheetName) -> Result<(), std::io::Error> { + let sheet_name = snake_case!(sheet_name.clone()); + + // Ensure sheet exists + let sheet_file_path = Sheet::sheet_path_with_name(self, &sheet_name); + if !sheet_file_path.exists() { + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("Sheet `{}` not found!", &sheet_name), + )); + } + + // Delete the sheet file + fs::remove_file(sheet_file_path).await?; + + Ok(()) + } + + /// Safely delete the sheet + /// + /// The sheet will be moved to the trash directory, ensuring it does not appear in the + /// results of `sheets` and `sheet_names` methods. + /// However, if the sheet's holder attempts to access the sheet through the `sheet` method, + /// the system will automatically restore it from the trash directory. + /// This means: the sheet will only permanently remain in the trash directory, + /// waiting for manual cleanup by an administrator, when it is truly no longer in use. + /// + /// This is a safer deletion method because it provides the possibility of recovery, + /// avoiding irreversible data loss caused by accidental deletion. + /// + /// Note: This function is intended for server-side use only and should not be + /// arbitrarily called by other members to prevent unauthorized data deletion. + pub async fn delete_sheet_safely(&self, sheet_name: &SheetName) -> Result<(), std::io::Error> { + let sheet_name = snake_case!(sheet_name.clone()); + + // Ensure the sheet exists + let sheet_file_path = Sheet::sheet_path_with_name(self, &sheet_name); + if !sheet_file_path.exists() { + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("Sheet `{}` not found!", &sheet_name), + )); + } + + // Create the trash directory + let trash_dir = self.vault_path.join(".trash"); + if !trash_dir.exists() { + fs::create_dir_all(&trash_dir).await?; + } + + // Generate a unique filename in the trash + let timestamp = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_millis(); + let trash_file_name = format!( + "{}_{}.{}", + sheet_name, timestamp, SERVER_SUFFIX_SHEET_FILE_NO_DOT + ); + let trash_path = trash_dir.join(trash_file_name); + + // Move the sheet file to the trash + fs::rename(&sheet_file_path, &trash_path).await?; + + Ok(()) + } + + /// Restore the sheet from the trash + /// + /// Restore the specified sheet from the trash to its original location, making it accessible normally. + pub async fn restore_sheet(&self, sheet_name: &SheetName) -> Result<(), std::io::Error> { + let sheet_name = snake_case!(sheet_name.clone()); + + // Search for matching files in the trash + let trash_dir = self.vault_path.join(".trash"); + if !trash_dir.exists() { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Trash directory does not exist!".to_string(), + )); + } + + let mut found_path = None; + for entry in std::fs::read_dir(&trash_dir)? { + let entry = entry?; + let path = entry.path(); + + if path.is_file() + && let Some(file_name) = path.file_stem().and_then(|s| s.to_str()) + { + // Check if the filename starts with the sheet name + if file_name.starts_with(&sheet_name) { + found_path = Some(path); + break; + } + } + } + + let trash_path = found_path.ok_or_else(|| { + Error::new( + std::io::ErrorKind::NotFound, + format!("Sheet `{}` not found in trash!", &sheet_name), + ) + })?; + + // Restore the sheet to its original location + let original_path = Sheet::sheet_path_with_name(self, &sheet_name); + fs::rename(&trash_path, &original_path).await?; + + Ok(()) + } +} diff --git a/legacy_data/src/data/vault/vault_config.rs b/legacy_data/src/data/vault/vault_config.rs new file mode 100644 index 0000000..caa8552 --- /dev/null +++ b/legacy_data/src/data/vault/vault_config.rs @@ -0,0 +1,233 @@ +use std::net::{IpAddr, Ipv4Addr}; + +use cfg_file::ConfigFile; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use crate::constants::{PORT, SERVER_FILE_VAULT}; +use crate::data::member::{Member, MemberId}; + +pub type VaultName = String; +pub type VaultUuid = Uuid; + +#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +pub enum AuthMode { + /// Use asymmetric keys: both client and server need to register keys, after which they can connect + Key, + + /// Use password: the password stays on the server, and the client needs to set the password locally for connection + #[default] + Password, + + /// No authentication: generally used in a strongly secure environment, skipping verification directly + NoAuth, +} + +#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +pub enum LoggerLevel { + Debug, + Trace, + + #[default] + Info, +} + +#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +pub enum ServiceEnabled { + Enable, + + #[default] + Disable, +} + +#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +pub enum BehaviourEnabled { + Yes, + + #[default] + No, +} + +impl Into for ServiceEnabled { + fn into(self) -> bool { + match self { + ServiceEnabled::Enable => true, + ServiceEnabled::Disable => false, + } + } +} + +impl Into for BehaviourEnabled { + fn into(self) -> bool { + match self { + BehaviourEnabled::Yes => true, + BehaviourEnabled::No => false, + } + } +} + +#[derive(Serialize, Deserialize, ConfigFile)] +#[cfg_file(path = SERVER_FILE_VAULT)] +pub struct VaultConfig { + /// Vault uuid, unique identifier for the vault + #[serde(rename = "uuid")] + vault_uuid: VaultUuid, + + /// Vault name, which can be used as the project name and generally serves as a hint + #[serde(rename = "name")] + vault_name: VaultName, + + /// Vault host ids, a list of member id representing administrator identities + #[serde(rename = "hosts")] + vault_host_list: Vec, + + /// Vault server configuration, which will be loaded when connecting to the server + #[serde(rename = "profile")] + server_config: VaultServerConfig, +} + +#[derive(Serialize, Deserialize)] +pub struct VaultServerConfig { + /// Local IP address to bind to when the server starts + #[serde(rename = "bind")] + local_bind: IpAddr, + + /// TCP port to bind to when the server starts + #[serde(rename = "port")] + port: u16, + + /// Enable logging + #[serde(rename = "logger")] + logger: Option, + + /// Logger Level + #[serde(rename = "logger_level")] + logger_level: Option, + + /// Whether to enable LAN discovery, allowing members on the same LAN to more easily find the upstream server + #[serde(rename = "lan_discovery")] + lan_discovery: Option, // TODO + + /// Authentication mode for the vault server + /// key: Use asymmetric keys for authentication + /// password: Use a password for authentication + /// noauth: No authentication required, requires a strongly secure environment + #[serde(rename = "auth_mode")] + auth_mode: Option, // TODO +} + +impl Default for VaultConfig { + fn default() -> Self { + Self { + vault_uuid: Uuid::new_v4(), + vault_name: "JustEnoughVault".to_string(), + vault_host_list: Vec::new(), + server_config: VaultServerConfig { + local_bind: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), + port: PORT, + logger: Some(BehaviourEnabled::default()), + logger_level: Some(LoggerLevel::default()), + lan_discovery: Some(ServiceEnabled::default()), + auth_mode: Some(AuthMode::Key), + }, + } + } +} + +/// Vault Management +impl VaultConfig { + /// Change name of the vault. + pub fn change_name(&mut self, name: impl Into) { + self.vault_name = name.into() + } + + /// Add admin + pub fn add_admin(&mut self, member: &Member) { + let uuid = member.id(); + if !self.vault_host_list.contains(&uuid) { + self.vault_host_list.push(uuid); + } + } + + /// Remove admin + pub fn remove_admin(&mut self, member: &Member) { + let id = member.id(); + self.vault_host_list.retain(|x| x != &id); + } + + /// Get vault UUID + pub fn vault_uuid(&self) -> &VaultUuid { + &self.vault_uuid + } + + /// Set vault UUID + pub fn set_vault_uuid(&mut self, vault_uuid: VaultUuid) { + self.vault_uuid = vault_uuid; + } + + /// Get vault name + pub fn vault_name(&self) -> &VaultName { + &self.vault_name + } + + /// Set vault name + pub fn set_vault_name(&mut self, vault_name: VaultName) { + self.vault_name = vault_name; + } + + /// Get vault admin list + pub fn vault_host_list(&self) -> &Vec { + &self.vault_host_list + } + + /// Set vault admin list + pub fn set_vault_host_list(&mut self, vault_host_list: Vec) { + self.vault_host_list = vault_host_list; + } + + /// Get server config + pub fn server_config(&self) -> &VaultServerConfig { + &self.server_config + } + + /// Set server config + pub fn set_server_config(&mut self, server_config: VaultServerConfig) { + self.server_config = server_config; + } +} + +impl VaultServerConfig { + /// Get local bind IP address + pub fn local_bind(&self) -> &IpAddr { + &self.local_bind + } + + /// Get port + pub fn port(&self) -> u16 { + self.port + } + + /// Check if LAN discovery is enabled + pub fn is_lan_discovery_enabled(&self) -> bool { + self.lan_discovery.clone().unwrap_or_default().into() + } + + /// Get logger enabled status + pub fn is_logger_enabled(&self) -> bool { + self.logger.clone().unwrap_or_default().into() + } + + /// Get logger level + pub fn logger_level(&self) -> LoggerLevel { + self.logger_level.clone().unwrap_or_default() + } + + /// Get authentication mode + pub fn auth_mode(&self) -> AuthMode { + self.auth_mode.clone().unwrap_or_default() + } +} diff --git a/legacy_data/src/data/vault/virtual_file.rs b/legacy_data/src/data/vault/virtual_file.rs new file mode 100644 index 0000000..28e9172 --- /dev/null +++ b/legacy_data/src/data/vault/virtual_file.rs @@ -0,0 +1,500 @@ +use std::{ + collections::HashMap, + io::{Error, ErrorKind}, + path::PathBuf, +}; + +use cfg_file::{ConfigFile, config::ConfigFile}; +use serde::{Deserialize, Serialize}; +use string_proc::{dot_case, snake_case}; +use tcp_connection::instance::ConnectionInstance; +use tokio::fs; +use uuid::Uuid; + +use crate::{ + constants::{ + DEFAULT_VF_DESCRIPTION, DEFAULT_VF_VERSION, KEY_TEMP_NAME, KEY_VF_ID, KEY_VF_INDEX, + KEY_VF_VERSION, SERVER_FILE_VF_META, SERVER_FILE_VF_VERSION_INSTANCE, SERVER_PATH_VF_ROOT, + SERVER_PATH_VF_STORAGE, SERVER_PATH_VF_TEMP, VF_PREFIX, + }, + data::{member::MemberId, vault::Vault}, +}; + +pub type VirtualFileId = String; +pub type VirtualFileVersion = String; + +pub struct VirtualFile<'a> { + /// Unique identifier for the virtual file + id: VirtualFileId, + + /// Reference of Vault + current_vault: &'a Vault, +} + +#[derive(Default, Clone, Serialize, Deserialize, ConfigFile)] +pub struct VirtualFileMeta { + /// Current version of the virtual file + #[serde(rename = "ver")] + current_version: VirtualFileVersion, + + /// The member who holds the edit right of the file + #[serde(rename = "holder")] + hold_member: MemberId, + + /// Description of each version + #[serde(rename = "descs")] + version_description: HashMap, + + /// Histories + #[serde(rename = "histories")] + histories: Vec, +} + +#[derive(Debug, Default, Clone, Serialize, Deserialize)] +pub struct VirtualFileVersionDescription { + /// The member who created this version + #[serde(rename = "creator")] + pub creator: MemberId, + + /// The description of this version + #[serde(rename = "desc")] + pub description: String, +} + +impl VirtualFileVersionDescription { + /// Create a new version description + pub fn new(creator: MemberId, description: String) -> Self { + Self { + creator, + description, + } + } +} + +/// Virtual File Operations +impl Vault { + /// Generate a temporary path for receiving + pub fn virtual_file_temp_path(&self) -> PathBuf { + let random_receive_name = format!("{}", uuid::Uuid::new_v4()); + self.vault_path + .join(SERVER_PATH_VF_TEMP.replace(KEY_TEMP_NAME, &random_receive_name)) + } + + /// Get the directory where virtual files are stored + pub fn virtual_file_storage_dir(&self) -> PathBuf { + self.vault_path().join(SERVER_PATH_VF_ROOT) + } + + /// Get the directory where a specific virtual file is stored + pub fn virtual_file_dir(&self, id: &VirtualFileId) -> Result { + Ok(self.vault_path().join( + SERVER_PATH_VF_STORAGE + .replace(KEY_VF_ID, &id.to_string()) + .replace(KEY_VF_INDEX, &Self::vf_index(id)?), + )) + } + + // Generate index path of virtual file + fn vf_index(id: &VirtualFileId) -> Result { + // Remove VF_PREFIX if present + let id_str = if let Some(stripped) = id.strip_prefix(VF_PREFIX) { + stripped + } else { + id + }; + + // Extract the first part before the first hyphen + let first_part = id_str.split('-').next().ok_or_else(|| { + std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "Invalid virtual file ID format: no hyphen found", + ) + })?; + + // Ensure the first part has at least 4 characters + if first_part.len() < 4 { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "Invalid virtual file ID format: first part must have at least 4 characters", + ))?; + } + + // Take only the first 4 characters and split into two 2-character chunks + let first_four = &first_part[0..4]; + let mut path = String::new(); + for i in (0..first_four.len()).step_by(2) { + if i > 0 { + path.push('/'); + } + path.push_str(&first_four[i..i + 2]); + } + + Ok(path) + } + + /// Get the directory where a specific virtual file's metadata is stored + pub fn virtual_file_real_path( + &self, + id: &VirtualFileId, + version: &VirtualFileVersion, + ) -> PathBuf { + self.vault_path().join( + SERVER_FILE_VF_VERSION_INSTANCE + .replace(KEY_VF_ID, &id.to_string()) + .replace(KEY_VF_INDEX, &Self::vf_index(id).unwrap_or_default()) + .replace(KEY_VF_VERSION, &version.to_string()), + ) + } + + /// Get the directory where a specific virtual file's metadata is stored + pub fn virtual_file_meta_path(&self, id: &VirtualFileId) -> PathBuf { + self.vault_path().join( + SERVER_FILE_VF_META + .replace(KEY_VF_ID, &id.to_string()) + .replace(KEY_VF_INDEX, &Self::vf_index(id).unwrap_or_default()), + ) + } + + /// Get the virtual file with the given ID + pub fn virtual_file(&self, id: &VirtualFileId) -> Result, std::io::Error> { + let dir = self.virtual_file_dir(id); + if dir?.exists() { + Ok(VirtualFile { + id: id.clone(), + current_vault: self, + }) + } else { + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "Cannot found virtual file!", + )) + } + } + + /// Get the meta data of the virtual file with the given ID + pub async fn virtual_file_meta( + &self, + id: &VirtualFileId, + ) -> Result { + let dir = self.virtual_file_meta_path(id); + let metadata = VirtualFileMeta::read_from(dir).await?; + Ok(metadata) + } + + /// Write the meta data of the virtual file with the given ID + pub async fn write_virtual_file_meta( + &self, + id: &VirtualFileId, + meta: &VirtualFileMeta, + ) -> Result<(), std::io::Error> { + let dir = self.virtual_file_meta_path(id); + VirtualFileMeta::write_to(meta, dir).await?; + Ok(()) + } + + /// Create a virtual file from a connection instance + /// + /// It's the only way to create virtual files! + /// + /// When the target machine executes `write_file`, use this function instead of `read_file`, + /// and provide the member ID of the transmitting member. + /// + /// The system will automatically receive the file and + /// create the virtual file. + pub async fn create_virtual_file_from_connection( + &self, + instance: &mut ConnectionInstance, + member_id: &MemberId, + ) -> Result { + let receive_path = self.virtual_file_temp_path(); + let new_id = format!("{}{}", VF_PREFIX, Uuid::new_v4()); + let move_path = self.virtual_file_real_path(&new_id, &DEFAULT_VF_VERSION.to_string()); + + match instance.read_file(receive_path.clone()).await { + Ok(_) => { + // Read successful, create virtual file + // Create default version description + let mut version_description = + HashMap::::new(); + version_description.insert( + DEFAULT_VF_VERSION.to_string(), + VirtualFileVersionDescription { + creator: member_id.clone(), + description: DEFAULT_VF_DESCRIPTION.to_string(), + }, + ); + // Create metadata + let mut meta = VirtualFileMeta { + current_version: DEFAULT_VF_VERSION.to_string(), + hold_member: member_id.clone(), // The holder of the newly created virtual file is the creator by default + version_description, + histories: Vec::default(), + }; + + // Add first version + meta.histories.push(DEFAULT_VF_VERSION.to_string()); + + // Write metadata to file + VirtualFileMeta::write_to(&meta, self.virtual_file_meta_path(&new_id)).await?; + + // Move temp file to virtual file directory + if let Some(parent) = move_path.parent() + && !parent.exists() + { + fs::create_dir_all(parent).await?; + } + fs::rename(receive_path, move_path).await?; + + Ok(new_id) + } + Err(e) => { + // Read failed, remove temp file. + if receive_path.exists() { + fs::remove_file(receive_path).await?; + } + + Err(Error::other(e)) + } + } + } + + /// Update a virtual file from a connection instance + /// + /// It's the only way to update virtual files! + /// When the target machine executes `write_file`, use this function instead of `read_file`, + /// and provide the member ID of the transmitting member. + /// + /// The system will automatically receive the file and + /// update the virtual file. + /// + /// Note: The specified member must hold the edit right of the file, + /// otherwise the file reception will not be allowed. + /// + /// Make sure to obtain the edit right of the file before calling this function. + pub async fn update_virtual_file_from_connection( + &self, + instance: &mut ConnectionInstance, + member: &MemberId, + virtual_file_id: &VirtualFileId, + new_version: &VirtualFileVersion, + description: VirtualFileVersionDescription, + ) -> Result<(), std::io::Error> { + let new_version = dot_case!(new_version.clone()); + let mut meta = self.virtual_file_meta(virtual_file_id).await?; + + // Check if the member has edit right + self.check_virtual_file_edit_right(member, virtual_file_id) + .await?; + + // Check if the new version already exists + if meta.version_description.contains_key(&new_version) { + return Err(Error::new( + ErrorKind::AlreadyExists, + format!( + "Version `{}` already exists for virtual file `{}`", + new_version, virtual_file_id + ), + )); + } + + // Verify success + let receive_path = self.virtual_file_temp_path(); + let move_path = self.virtual_file_real_path(virtual_file_id, &new_version); + + match instance.read_file(receive_path.clone()).await { + Ok(_) => { + // Read success, move temp file to real path. + fs::rename(receive_path, move_path).await?; + + // Update metadata + meta.current_version = new_version.clone(); + meta.version_description + .insert(new_version.clone(), description); + meta.histories.push(new_version); + VirtualFileMeta::write_to(&meta, self.virtual_file_meta_path(virtual_file_id)) + .await?; + + Ok(()) + } + Err(e) => { + // Read failed, remove temp file. + if receive_path.exists() { + fs::remove_file(receive_path).await?; + } + + Err(Error::other(e)) + } + } + } + + /// Update virtual file from existing version + /// + /// This operation creates a new version based on the specified old version file instance. + /// The new version will retain the same version name as the old version, but use a different version number. + /// After the update, this version will be considered newer than the original version when comparing versions. + pub async fn update_virtual_file_from_exist_version( + &self, + member: &MemberId, + virtual_file_id: &VirtualFileId, + old_version: &VirtualFileVersion, + ) -> Result<(), std::io::Error> { + let old_version = snake_case!(old_version.clone()); + let mut meta = self.virtual_file_meta(virtual_file_id).await?; + + // Check if the member has edit right + self.check_virtual_file_edit_right(member, virtual_file_id) + .await?; + + // Ensure virtual file exist + let Ok(_) = self.virtual_file(virtual_file_id) else { + return Err(Error::new( + ErrorKind::NotFound, + format!("Virtual file `{}` not found!", virtual_file_id), + )); + }; + + // Ensure version exist + if !meta.version_exists(&old_version) { + return Err(Error::new( + ErrorKind::NotFound, + format!("Version `{}` not found!", old_version), + )); + } + + // Ok, Create new version + meta.current_version = old_version.clone(); + meta.histories.push(old_version); + VirtualFileMeta::write_to(&meta, self.virtual_file_meta_path(virtual_file_id)).await?; + + Ok(()) + } + + /// Grant a member the edit right for a virtual file + /// This operation takes effect immediately upon success + pub async fn grant_virtual_file_edit_right( + &self, + member_id: &MemberId, + virtual_file_id: &VirtualFileId, + ) -> Result<(), std::io::Error> { + let mut meta = self.virtual_file_meta(virtual_file_id).await?; + meta.hold_member = member_id.clone(); + self.write_virtual_file_meta(virtual_file_id, &meta).await + } + + /// Check if a member has the edit right for a virtual file + pub async fn has_virtual_file_edit_right( + &self, + member_id: &MemberId, + virtual_file_id: &VirtualFileId, + ) -> Result { + let meta = self.virtual_file_meta(virtual_file_id).await?; + Ok(meta.hold_member.eq(member_id)) + } + + /// Check if a member has the edit right for a virtual file and return Result + /// Returns Ok(()) if the member has edit right, otherwise returns PermissionDenied error + pub async fn check_virtual_file_edit_right( + &self, + member_id: &MemberId, + virtual_file_id: &VirtualFileId, + ) -> Result<(), std::io::Error> { + if !self + .has_virtual_file_edit_right(member_id, virtual_file_id) + .await? + { + return Err(Error::new( + ErrorKind::PermissionDenied, + format!( + "Member `{}` not allowed to update virtual file `{}`", + member_id, virtual_file_id + ), + )); + } + Ok(()) + } + + /// Revoke the edit right for a virtual file from the current holder + /// This operation takes effect immediately upon success + pub async fn revoke_virtual_file_edit_right( + &self, + virtual_file_id: &VirtualFileId, + ) -> Result<(), std::io::Error> { + let mut meta = self.virtual_file_meta(virtual_file_id).await?; + meta.hold_member = String::default(); + self.write_virtual_file_meta(virtual_file_id, &meta).await + } +} + +impl<'a> VirtualFile<'a> { + /// Get id of VirtualFile + pub fn id(&self) -> VirtualFileId { + self.id.clone() + } + + /// Read metadata of VirtualFile + pub async fn read_meta(&self) -> Result { + self.current_vault.virtual_file_meta(&self.id).await + } +} + +impl VirtualFileMeta { + /// Get all versions of the virtual file + pub fn versions(&self) -> &Vec { + &self.histories + } + + /// Get the latest version of the virtual file + pub fn version_latest(&self) -> VirtualFileVersion { + // After creating a virtual file in `update_virtual_file_from_connection`, + // the Vec will never be empty, so unwrap is allowed here + self.histories.last().unwrap().clone() + } + + /// Get the total number of versions for this virtual file + pub fn version_len(&self) -> i32 { + self.histories.len() as i32 + } + + /// Check if a specific version exists + /// Returns true if the version exists, false otherwise + pub fn version_exists(&self, version: &VirtualFileVersion) -> bool { + self.versions().iter().any(|v| v == version) + } + + /// Get the version number (index) for a given version name + /// Returns None if the version doesn't exist + pub fn version_num(&self, version: &VirtualFileVersion) -> Option { + self.histories + .iter() + .rev() + .position(|v| v == version) + .map(|pos| (self.histories.len() - 1 - pos) as i32) + } + + /// Get the version name for a given version number (index) + /// Returns None if the version number is out of range + pub fn version_name(&self, version_num: i32) -> Option { + self.histories.get(version_num as usize).cloned() + } + + /// Get the member who holds the edit right of the file + pub fn hold_member(&self) -> &MemberId { + &self.hold_member + } + + /// Get the version descriptions for all versions + pub fn version_descriptions( + &self, + ) -> &HashMap { + &self.version_description + } + + /// Get the version description for a given version + pub fn version_description( + &self, + version: VirtualFileVersion, + ) -> Option<&VirtualFileVersionDescription> { + let desc = self.version_descriptions(); + desc.get(&version) + } +} diff --git a/legacy_data/src/env.rs b/legacy_data/src/env.rs new file mode 100644 index 0000000..209c0cc --- /dev/null +++ b/legacy_data/src/env.rs @@ -0,0 +1,84 @@ +use crate::constants::*; +use std::io::{self, Error}; +use std::{env::set_current_dir, path::PathBuf}; + +/// Find the nearest vault or local workspace and correct the `current_dir` to it +pub fn correct_current_dir() -> Result<(), io::Error> { + if let Some(local_workspace) = current_local_path() { + set_current_dir(local_workspace)?; + return Ok(()); + } + if let Some(vault) = current_vault_path() { + set_current_dir(vault)?; + return Ok(()); + } + Err(Error::new( + io::ErrorKind::NotFound, + "Could not find any vault or local workspace!", + )) +} + +/// Get the nearest Vault directory from `current_dir` +pub fn current_vault_path() -> Option { + let current_dir = std::env::current_dir().ok()?; + find_vault_path(current_dir) +} + +/// Get the nearest local workspace from `current_dir` +pub fn current_local_path() -> Option { + let current_dir = std::env::current_dir().ok()?; + find_local_path(current_dir) +} + +/// Get the nearest Vault directory from the specified path +pub fn find_vault_path(path: impl Into) -> Option { + let mut current_path = path.into(); + let vault_file = SERVER_FILE_VAULT; + + loop { + let vault_toml_path = current_path.join(vault_file); + if vault_toml_path.exists() { + return Some(current_path); + } + + if let Some(parent) = current_path.parent() { + current_path = parent.to_path_buf(); + } else { + break; + } + } + + None +} + +/// Get the nearest local workspace from the specified path +pub fn find_local_path(path: impl Into) -> Option { + let mut current_path = path.into(); + let workspace_dir = CLIENT_PATH_WORKSPACE_ROOT; + + loop { + let jvc_path = current_path.join(workspace_dir); + if jvc_path.exists() { + return Some(current_path); + } + + if let Some(parent) = current_path.parent() { + current_path = parent.to_path_buf(); + } else { + break; + } + } + + None +} + +/// Get the system's document directory and join with the appropriate application name +pub fn current_cfg_dir() -> Option { + dirs::config_local_dir().map(|path| { + if cfg!(target_os = "linux") { + path.join("jvcs") + } else { + path.join("JustEnoughVCS") + } + }) +} diff --git a/legacy_data/src/lib.rs b/legacy_data/src/lib.rs new file mode 100644 index 0000000..df2ec34 --- /dev/null +++ b/legacy_data/src/lib.rs @@ -0,0 +1,5 @@ +pub mod constants; +pub mod env; + +#[allow(dead_code)] +pub mod data; diff --git a/legacy_data/tests/Cargo.toml b/legacy_data/tests/Cargo.toml new file mode 100644 index 0000000..b022436 --- /dev/null +++ b/legacy_data/tests/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "vcs_data_test" +edition = "2024" +version.workspace = true + +[dependencies] +tcp_connection = { path = "../../utils/tcp_connection" } +tcp_connection_test = { path = "../../utils/tcp_connection/tcp_connection_test" } +cfg_file = { path = "../../utils/cfg_file", features = ["default"] } +vcs_data = { path = "../../legacy_data" } + +# Async & Networking +tokio = { version = "1.48.0", features = ["full"] } diff --git a/legacy_data/tests/src/lib.rs b/legacy_data/tests/src/lib.rs new file mode 100644 index 0000000..ced2d3d --- /dev/null +++ b/legacy_data/tests/src/lib.rs @@ -0,0 +1,30 @@ +use std::{env::current_dir, path::PathBuf}; + +use tokio::fs; + +#[cfg(test)] +pub mod test_vault_setup_and_member_register; + +#[cfg(test)] +pub mod test_virtual_file_creation_and_update; + +#[cfg(test)] +pub mod test_local_workspace_setup_and_account_management; + +#[cfg(test)] +pub mod test_sheet_creation_management_and_persistence; + +#[cfg(test)] +pub mod test_sheet_share_creation_and_management; + +pub async fn get_test_dir(area: &str) -> Result { + let dir = current_dir()?.join(".temp").join("test").join(area); + if !dir.exists() { + std::fs::create_dir_all(&dir)?; + } else { + // Regenerate existing directory + fs::remove_dir_all(&dir).await?; + fs::create_dir_all(&dir).await?; + } + Ok(dir) +} diff --git a/legacy_data/tests/src/test_local_workspace_setup_and_account_management.rs b/legacy_data/tests/src/test_local_workspace_setup_and_account_management.rs new file mode 100644 index 0000000..edb9295 --- /dev/null +++ b/legacy_data/tests/src/test_local_workspace_setup_and_account_management.rs @@ -0,0 +1,248 @@ +use std::io::Error; + +use cfg_file::config::ConfigFile; +use vcs_data::{ + constants::{CLIENT_FILE_TODOLIST, CLIENT_FILE_WORKSPACE, USER_FILE_KEY, USER_FILE_MEMBER}, + data::{ + local::{LocalWorkspace, workspace_config::LocalConfig}, + member::Member, + user::UserDirectory, + }, +}; + +use crate::get_test_dir; + +#[tokio::test] +async fn test_local_workspace_setup_and_account_management() -> Result<(), std::io::Error> { + let dir = get_test_dir("local_workspace_account_management").await?; + + // Setup local workspace + LocalWorkspace::setup_local_workspace(dir.clone()).await?; + + // Check if the following files are created in `dir`: + // Files: CLIENT_FILE_WORKSPACE, CLIENT_FILE_README + assert!(dir.join(CLIENT_FILE_WORKSPACE).exists()); + assert!(dir.join(CLIENT_FILE_TODOLIST).exists()); + + // Get local workspace + let config = LocalConfig::read_from(dir.join(CLIENT_FILE_WORKSPACE)).await?; + let Some(_local_workspace) = LocalWorkspace::init(config, &dir) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Local workspace not found!", + )); + }; + + // Create user directory from workspace path + let Some(user_directory) = UserDirectory::from_path(&dir) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "User directory not found!", + )); + }; + + // Test account registration + let member_id = "test_account"; + let member = Member::new(member_id); + + // Register account + user_directory.register_account(member.clone()).await?; + + // Check if the account config file exists + assert!( + dir.join(USER_FILE_MEMBER.replace("{self_id}", member_id)) + .exists() + ); + + // Test account retrieval + let retrieved_member = user_directory.account(&member_id.to_string()).await?; + assert_eq!(retrieved_member.id(), member.id()); + + // Test account IDs listing + let account_ids = user_directory.account_ids()?; + assert!(account_ids.contains(&member_id.to_string())); + + // Test accounts listing + let accounts = user_directory.accounts().await?; + assert_eq!(accounts.len(), 1); + assert_eq!(accounts[0].id(), member.id()); + + // Test account existence check + assert!(user_directory.account_cfg(&member_id.to_string()).is_some()); + + // Test private key check (should be false initially) + assert!(!user_directory.has_private_key(&member_id.to_string())); + + // Test account update + let mut updated_member = member.clone(); + updated_member.set_metadata("email", "test@example.com"); + user_directory + .update_account(updated_member.clone()) + .await?; + + // Verify update + let updated_retrieved = user_directory.account(&member_id.to_string()).await?; + assert_eq!( + updated_retrieved.metadata("email"), + Some(&"test@example.com".to_string()) + ); + + // Test account removal + user_directory.remove_account(&member_id.to_string())?; + + // Check if the account config file no longer exists + assert!( + !dir.join(USER_FILE_MEMBER.replace("{self_id}", member_id)) + .exists() + ); + + // Check if account is no longer in the list + let account_ids_after_removal = user_directory.account_ids()?; + assert!(!account_ids_after_removal.contains(&member_id.to_string())); + + Ok(()) +} + +#[tokio::test] +async fn test_account_private_key_management() -> Result<(), std::io::Error> { + let dir = get_test_dir("account_private_key_management").await?; + + // Create user directory + let Some(user_directory) = UserDirectory::from_path(&dir) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "User directory not found!", + )); + }; + + // Register account + let member_id = "test_account_with_key"; + let member = Member::new(member_id); + user_directory.register_account(member).await?; + + // Create a dummy private key file for testing + let private_key_path = dir.join(USER_FILE_KEY.replace("{self_id}", member_id)); + std::fs::create_dir_all(private_key_path.parent().unwrap())?; + std::fs::write(&private_key_path, "dummy_private_key_content")?; + + // Test private key existence check + assert!(user_directory.has_private_key(&member_id.to_string())); + + // Test private key path retrieval + assert!( + user_directory + .account_private_key(&member_id.to_string()) + .is_some() + ); + + // Remove account (should also remove private key) + user_directory.remove_account(&member_id.to_string())?; + + // Check if private key file is also removed + assert!(!private_key_path.exists()); + + Ok(()) +} + +#[tokio::test] +async fn test_multiple_account_management() -> Result<(), std::io::Error> { + let dir = get_test_dir("multiple_account_management").await?; + + // Create user directory + let Some(user_directory) = UserDirectory::from_path(&dir) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "User directory not found!", + )); + }; + + // Register multiple accounts + let account_names = vec!["alice", "bob", "charlie"]; + + for name in &account_names { + user_directory.register_account(Member::new(*name)).await?; + } + + // Test account IDs listing + let account_ids = user_directory.account_ids()?; + assert_eq!(account_ids.len(), 3); + + for name in &account_names { + assert!(account_ids.contains(&name.to_string())); + } + + // Test accounts listing + let accounts = user_directory.accounts().await?; + assert_eq!(accounts.len(), 3); + + // Remove one account + user_directory.remove_account(&"bob".to_string())?; + + // Verify removal + let account_ids_after_removal = user_directory.account_ids()?; + assert_eq!(account_ids_after_removal.len(), 2); + assert!(!account_ids_after_removal.contains(&"bob".to_string())); + assert!(account_ids_after_removal.contains(&"alice".to_string())); + assert!(account_ids_after_removal.contains(&"charlie".to_string())); + + Ok(()) +} + +#[tokio::test] +async fn test_account_registration_duplicate_prevention() -> Result<(), std::io::Error> { + let dir = get_test_dir("account_duplicate_prevention").await?; + + // Create user directory + let Some(user_directory) = UserDirectory::from_path(&dir) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "User directory not found!", + )); + }; + + // Register account + let member_id = "duplicate_test"; + user_directory + .register_account(Member::new(member_id)) + .await?; + + // Try to register same account again - should fail + let result = user_directory + .register_account(Member::new(member_id)) + .await; + assert!(result.is_err()); + + Ok(()) +} + +#[tokio::test] +async fn test_nonexistent_account_operations() -> Result<(), std::io::Error> { + let dir = get_test_dir("nonexistent_account_operations").await?; + + // Create user directory + let Some(user_directory) = UserDirectory::from_path(&dir) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "User directory not found!", + )); + }; + + // Try to read non-existent account - should fail + let result = user_directory.account(&"nonexistent".to_string()).await; + assert!(result.is_err()); + + // Try to update non-existent account - should fail + let result = user_directory + .update_account(Member::new("nonexistent")) + .await; + assert!(result.is_err()); + + // Try to remove non-existent account - should succeed (idempotent) + let result = user_directory.remove_account(&"nonexistent".to_string()); + assert!(result.is_ok()); + + // Check private key for non-existent account - should be false + assert!(!user_directory.has_private_key(&"nonexistent".to_string())); + + Ok(()) +} diff --git a/legacy_data/tests/src/test_sheet_creation_management_and_persistence.rs b/legacy_data/tests/src/test_sheet_creation_management_and_persistence.rs new file mode 100644 index 0000000..adff1bd --- /dev/null +++ b/legacy_data/tests/src/test_sheet_creation_management_and_persistence.rs @@ -0,0 +1,275 @@ +use std::io::Error; + +use cfg_file::config::ConfigFile; +use vcs_data::{ + constants::{SERVER_FILE_SHEET, SERVER_FILE_VAULT}, + data::{ + member::{Member, MemberId}, + sheet::SheetName, + vault::{Vault, vault_config::VaultConfig, virtual_file::VirtualFileId}, + }, +}; + +use crate::get_test_dir; + +#[tokio::test] +async fn test_sheet_creation_management_and_persistence() -> Result<(), std::io::Error> { + let dir = get_test_dir("sheet_management").await?; + + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await?; + + // Get vault + let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; + let Some(vault) = Vault::init(config, &dir) else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); + }; + + // Add a member to use as sheet holder + let member_id: MemberId = "test_member".to_string(); + vault + .register_member_to_vault(Member::new(&member_id)) + .await?; + + // Test 1: Create a new sheet + let sheet_name: SheetName = "test_sheet".to_string(); + let sheet = vault.create_sheet(&sheet_name, &member_id).await?; + + // Verify sheet properties + assert_eq!(sheet.holder(), Some(&member_id)); + assert_eq!(sheet.holder(), Some(&member_id)); + assert!(sheet.mapping().is_empty()); + + // Verify sheet file was created + let sheet_path = dir.join(SERVER_FILE_SHEET.replace("{sheet_name}", &sheet_name)); + assert!(sheet_path.exists()); + + // Test 2: Add mapping entries to the sheet + let mut sheet = vault.sheet(&sheet_name).await?; + + // Add mapping entries for the files + let main_rs_path = vcs_data::data::sheet::SheetPathBuf::from("src/main.rs"); + let lib_rs_path = vcs_data::data::sheet::SheetPathBuf::from("src/lib.rs"); + let main_rs_id = VirtualFileId::new(); + let lib_rs_id = VirtualFileId::new(); + + sheet + .add_mapping( + main_rs_path.clone(), + main_rs_id.clone(), + "1.0.0".to_string(), + ) + .await?; + sheet + .add_mapping(lib_rs_path.clone(), lib_rs_id.clone(), "1.0.0".to_string()) + .await?; + + // Verify mappings were added + assert_eq!(sheet.mapping().len(), 2); + + // Test 3: Add more mapping entries + let mapping_path = vcs_data::data::sheet::SheetPathBuf::from("output/build.exe"); + let virtual_file_id = VirtualFileId::new(); + + sheet + .add_mapping( + mapping_path.clone(), + virtual_file_id.clone(), + "1.0.0".to_string(), + ) + .await?; + + // Verify mapping was added + assert_eq!(sheet.mapping().len(), 3); + assert_eq!( + sheet.mapping().get(&mapping_path).map(|meta| &meta.id), + Some(&virtual_file_id) + ); + + // Test 4: Persist sheet to disk + sheet.persist().await?; + + // Verify persistence by reloading the sheet + let reloaded_sheet = vault.sheet(&sheet_name).await?; + assert_eq!(reloaded_sheet.holder(), Some(&member_id)); + assert_eq!(reloaded_sheet.mapping().len(), 3); + + // Test 5: Remove mapping entry + let mut sheet_for_removal = vault.sheet(&sheet_name).await?; + let _removed_virtual_file_id = sheet_for_removal.remove_mapping(&mapping_path).await; + // Don't check the return value since it depends on virtual file existence + assert_eq!(sheet_for_removal.mapping().len(), 2); + + // Test 6: List all sheets in vault + let sheet_names = vault.sheet_names()?; + assert_eq!(sheet_names.len(), 2); + assert!(sheet_names.contains(&sheet_name)); + assert!(sheet_names.contains(&"ref".to_string())); + + let all_sheets = vault.sheets().await?; + assert_eq!(all_sheets.len(), 2); + // One sheet should be the test sheet, the other should be the ref sheet with host as holder + let test_sheet_holder = all_sheets + .iter() + .find(|s| s.holder() == Some(&member_id)) + .map(|s| s.holder()) + .unwrap(); + let ref_sheet_holder = all_sheets + .iter() + .find(|s| s.holder() == Some(&"host".to_string())) + .map(|s| s.holder()) + .unwrap(); + assert_eq!(test_sheet_holder, Some(&member_id)); + assert_eq!(ref_sheet_holder, Some(&"host".to_string())); + + // Test 7: Safe deletion (move to trash) + vault.delete_sheet_safely(&sheet_name).await?; + + // Verify sheet is not in normal listing but can be restored + let sheet_names_after_deletion = vault.sheet_names()?; + assert_eq!(sheet_names_after_deletion.len(), 1); + assert_eq!(sheet_names_after_deletion[0], "ref"); + + // Test 8: Restore sheet from trash + let restored_sheet = vault.sheet(&sheet_name).await?; + assert_eq!(restored_sheet.holder(), Some(&member_id)); + assert_eq!(restored_sheet.holder(), Some(&member_id)); + + // Verify sheet is back in normal listing + let sheet_names_after_restore = vault.sheet_names()?; + assert_eq!(sheet_names_after_restore.len(), 2); + assert!(sheet_names_after_restore.contains(&sheet_name)); + assert!(sheet_names_after_restore.contains(&"ref".to_string())); + + // Test 9: Permanent deletion + vault.delete_sheet(&sheet_name).await?; + + // Verify sheet is permanently gone + let sheet_names_final = vault.sheet_names()?; + assert_eq!(sheet_names_final.len(), 1); + assert_eq!(sheet_names_final[0], "ref"); + + // Attempt to access deleted sheet should fail + let result = vault.sheet(&sheet_name).await; + assert!(result.is_err()); + + // Clean up: Remove member + vault.remove_member_from_vault(&member_id)?; + + Ok(()) +} + +#[tokio::test] +async fn test_sheet_error_conditions() -> Result<(), std::io::Error> { + let dir = get_test_dir("sheet_error_conditions").await?; + + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await?; + + // Get vault + let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; + let Some(vault) = Vault::init(config, &dir) else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); + }; + + // Test 1: Create sheet with non-existent member should fail + let non_existent_member: MemberId = "non_existent_member".to_string(); + let sheet_name: SheetName = "test_sheet".to_string(); + + let result = vault.create_sheet(&sheet_name, &non_existent_member).await; + assert!(result.is_err()); + + // Add a member first + let member_id: MemberId = "test_member".to_string(); + vault + .register_member_to_vault(Member::new(&member_id)) + .await?; + + // Test 2: Create duplicate sheet should fail + vault.create_sheet(&sheet_name, &member_id).await?; + let result = vault.create_sheet(&sheet_name, &member_id).await; + assert!(result.is_err()); + + // Test 3: Delete non-existent sheet should fail + let non_existent_sheet: SheetName = "non_existent_sheet".to_string(); + let result = vault.delete_sheet(&non_existent_sheet).await; + assert!(result.is_err()); + + // Test 4: Safe delete non-existent sheet should fail + let result = vault.delete_sheet_safely(&non_existent_sheet).await; + assert!(result.is_err()); + + // Test 5: Restore non-existent sheet from trash should fail + let result = vault.restore_sheet(&non_existent_sheet).await; + assert!(result.is_err()); + + // Clean up + vault.remove_member_from_vault(&member_id)?; + + Ok(()) +} + +#[tokio::test] +async fn test_sheet_data_serialization() -> Result<(), std::io::Error> { + let dir = get_test_dir("sheet_serialization").await?; + + // Test serialization by creating a sheet through the vault + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await?; + + // Get vault + let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; + let Some(vault) = Vault::init(config, &dir) else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); + }; + + // Add a member + let member_id: MemberId = "test_member".to_string(); + vault + .register_member_to_vault(Member::new(&member_id)) + .await?; + + // Create a sheet + let sheet_name: SheetName = "test_serialization_sheet".to_string(); + let mut sheet = vault.create_sheet(&sheet_name, &member_id).await?; + + // Add some mappings + let main_rs_path = vcs_data::data::sheet::SheetPathBuf::from("src/main.rs"); + let lib_rs_path = vcs_data::data::sheet::SheetPathBuf::from("src/lib.rs"); + let main_rs_id = VirtualFileId::new(); + let lib_rs_id = VirtualFileId::new(); + + sheet + .add_mapping( + main_rs_path.clone(), + main_rs_id.clone(), + "1.0.0".to_string(), + ) + .await?; + sheet + .add_mapping(lib_rs_path.clone(), lib_rs_id.clone(), "1.0.0".to_string()) + .await?; + + // Add more mappings + let build_exe_id = VirtualFileId::new(); + + sheet + .add_mapping( + vcs_data::data::sheet::SheetPathBuf::from("output/build.exe"), + build_exe_id, + "1.0.0".to_string(), + ) + .await?; + + // Persist the sheet + sheet.persist().await?; + + // Verify the sheet file was created + let sheet_path = dir.join(SERVER_FILE_SHEET.replace("{sheet_name}", &sheet_name)); + assert!(sheet_path.exists()); + + // Clean up + vault.remove_member_from_vault(&member_id)?; + + Ok(()) +} diff --git a/legacy_data/tests/src/test_sheet_share_creation_and_management.rs b/legacy_data/tests/src/test_sheet_share_creation_and_management.rs new file mode 100644 index 0000000..5123360 --- /dev/null +++ b/legacy_data/tests/src/test_sheet_share_creation_and_management.rs @@ -0,0 +1,631 @@ +use std::io::Error; + +use cfg_file::config::ConfigFile; +use vcs_data::{ + constants::SERVER_FILE_VAULT, + data::{ + member::{Member, MemberId}, + sheet::{SheetName, SheetPathBuf}, + vault::{ + Vault, + mapping_share::{Share, ShareMergeMode, SheetShareId}, + vault_config::VaultConfig, + virtual_file::VirtualFileId, + }, + }, +}; + +use crate::get_test_dir; + +#[tokio::test] +async fn test_share_creation_and_retrieval() -> Result<(), std::io::Error> { + let dir = get_test_dir("share_creation").await?; + + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await?; + + // Get vault + let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; + let Some(vault) = Vault::init(config, &dir) else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); + }; + + // Add members + let sharer_id: MemberId = "sharer_member".to_string(); + let target_member_id: MemberId = "target_member".to_string(); + + vault + .register_member_to_vault(Member::new(&sharer_id)) + .await?; + vault + .register_member_to_vault(Member::new(&target_member_id)) + .await?; + + // Create source sheet for sharer + let source_sheet_name: SheetName = "source_sheet".to_string(); + let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; + + // Create target sheet for target member + let target_sheet_name: SheetName = "target_sheet".to_string(); + let _target_sheet = vault + .create_sheet(&target_sheet_name, &target_member_id) + .await?; + + // Add mappings to source sheet + let mut source_sheet = vault.sheet(&source_sheet_name).await?; + + let main_rs_path = SheetPathBuf::from("src/main.rs"); + let lib_rs_path = SheetPathBuf::from("src/lib.rs"); + let main_rs_id = VirtualFileId::from("main_rs_id_1"); + let lib_rs_id = VirtualFileId::from("lib_rs_id_1"); + + source_sheet + .add_mapping( + main_rs_path.clone(), + main_rs_id.clone(), + "1.0.0".to_string(), + ) + .await?; + source_sheet + .add_mapping(lib_rs_path.clone(), lib_rs_id.clone(), "1.0.0".to_string()) + .await?; + + // Persist source sheet + source_sheet.persist().await?; + + // Test 1: Share mappings from source sheet to target sheet + let description = "Test share of main.rs and lib.rs".to_string(); + // Need to get the sheet again after persist + let source_sheet = vault.sheet(&source_sheet_name).await?; + + source_sheet + .share_mappings( + &target_sheet_name, + vec![main_rs_path.clone(), lib_rs_path.clone()], + &sharer_id, + description.clone(), + ) + .await?; + + // Test 2: Get shares from target sheet + let target_sheet = vault.sheet(&target_sheet_name).await?; + + let shares = target_sheet.get_shares().await?; + + assert_eq!(shares.len(), 1, "Expected 1 share, found {}", shares.len()); + let share = &shares[0]; + + assert_eq!(share.sharer, sharer_id); + assert_eq!(share.description, description); + assert_eq!(share.from_sheet, source_sheet_name); + assert_eq!(share.mappings.len(), 2); + assert!(share.mappings.contains_key(&main_rs_path)); + assert!(share.mappings.contains_key(&lib_rs_path)); + assert!(share.path.is_some()); + + // Test 3: Get specific share by ID + let share_id = Share::gen_share_id(&sharer_id); + let _specific_share = target_sheet.get_share(&share_id).await; + + // Note: The share ID might not match exactly due to random generation, + // but we can verify the share exists by checking the shares list + assert!(shares.iter().any(|s| s.sharer == sharer_id)); + + // Clean up + vault.remove_member_from_vault(&sharer_id)?; + vault.remove_member_from_vault(&target_member_id)?; + + Ok(()) +} + +#[tokio::test] +async fn test_share_merge_modes() -> Result<(), std::io::Error> { + let dir = get_test_dir("share_merge_modes").await?; + + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await?; + + // Get vault + let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; + let Some(vault) = Vault::init(config, &dir) else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); + }; + + // Add members + let sharer_id: MemberId = "sharer".to_string(); + let target_member_id: MemberId = "target".to_string(); + + vault + .register_member_to_vault(Member::new(&sharer_id)) + .await?; + vault + .register_member_to_vault(Member::new(&target_member_id)) + .await?; + + // Create source and target sheets + let source_sheet_name: SheetName = "source".to_string(); + let target_sheet_name: SheetName = "target".to_string(); + + let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; + let _target_sheet = vault + .create_sheet(&target_sheet_name, &target_member_id) + .await?; + + // Add mappings to source sheet + let mut source_sheet = vault.sheet(&source_sheet_name).await?; + + let file1_path = SheetPathBuf::from("src/file1.rs"); + let file2_path = SheetPathBuf::from("src/file2.rs"); + let file1_id = VirtualFileId::from("file1_id_1"); + let file2_id = VirtualFileId::from("file2_id_1"); + + source_sheet + .add_mapping(file1_path.clone(), file1_id.clone(), "1.0.0".to_string()) + .await?; + source_sheet + .add_mapping(file2_path.clone(), file2_id.clone(), "1.0.0".to_string()) + .await?; + + source_sheet.persist().await?; + + // Share mappings + // Need to get the sheet again after persist + let source_sheet = vault.sheet(&source_sheet_name).await?; + source_sheet + .share_mappings( + &target_sheet_name, + vec![file1_path.clone(), file2_path.clone()], + &sharer_id, + "Test share".to_string(), + ) + .await?; + + // Get the share + let target_sheet = vault.sheet(&target_sheet_name).await?; + let shares = target_sheet.get_shares().await?; + assert_eq!(shares.len(), 1); + let share = shares[0].clone(); + + // Test 4: Safe mode merge (should succeed with no conflicts) + let result = target_sheet + .merge_share(share.clone(), ShareMergeMode::Safe) + .await; + + assert!( + result.is_ok(), + "Safe mode should succeed with no conflicts " + ); + + // Verify mappings were added to target sheet + let updated_target_sheet = vault.sheet(&target_sheet_name).await?; + assert_eq!(updated_target_sheet.mapping().len(), 2); + assert!(updated_target_sheet.mapping().contains_key(&file1_path)); + assert!(updated_target_sheet.mapping().contains_key(&file2_path)); + + // Clean up + vault.remove_member_from_vault(&sharer_id)?; + vault.remove_member_from_vault(&target_member_id)?; + + Ok(()) +} + +#[tokio::test] +async fn test_share_merge_conflicts() -> Result<(), std::io::Error> { + let dir = get_test_dir("share_conflicts").await?; + + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await?; + + // Get vault + let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; + let Some(vault) = Vault::init(config, &dir) else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); + }; + + // Add members + let sharer_id: MemberId = "sharer".to_string(); + let target_member_id: MemberId = "target".to_string(); + + vault + .register_member_to_vault(Member::new(&sharer_id)) + .await?; + vault + .register_member_to_vault(Member::new(&target_member_id)) + .await?; + + // Create source and target sheets + let source_sheet_name: SheetName = "source".to_string(); + let target_sheet_name: SheetName = "target".to_string(); + + let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; + let _target_sheet = vault + .create_sheet(&target_sheet_name, &target_member_id) + .await?; + + // Add conflicting mappings to both sheets + let mut source_sheet = vault.sheet(&source_sheet_name).await?; + let mut target_sheet_mut = vault.sheet(&target_sheet_name).await?; + + let conflicting_path = SheetPathBuf::from("src/conflicting.rs"); + let source_file_id = VirtualFileId::from("source_file_id_1"); + let target_file_id = VirtualFileId::from("target_file_id_1"); + + // Add same path with different IDs to both sheets (conflict) + source_sheet + .add_mapping( + conflicting_path.clone(), + source_file_id.clone(), + "1.0.0".to_string(), + ) + .await?; + + target_sheet_mut + .add_mapping( + conflicting_path.clone(), + target_file_id.clone(), + "1.0.0".to_string(), + ) + .await?; + + source_sheet.persist().await?; + target_sheet_mut.persist().await?; + + // Share the conflicting mapping + // Need to get the sheet again after persist + let source_sheet = vault.sheet(&source_sheet_name).await?; + source_sheet + .share_mappings( + &target_sheet_name, + vec![conflicting_path.clone()], + &sharer_id, + "Conflicting share".to_string(), + ) + .await?; + + // Get the share + let target_sheet = vault.sheet(&target_sheet_name).await?; + let shares = target_sheet.get_shares().await?; + assert_eq!(shares.len(), 1); + let share = shares[0].clone(); + + // Test 5: Safe mode merge with conflict (should fail) + let target_sheet_clone = vault.sheet(&target_sheet_name).await?; + let result = target_sheet_clone + .merge_share(share.clone(), ShareMergeMode::Safe) + .await; + + assert!(result.is_err(), "Safe mode should fail with conflicts"); + + // Test 6: Overwrite mode merge with conflict (should succeed) + let target_sheet_clone = vault.sheet(&target_sheet_name).await?; + let result = target_sheet_clone + .merge_share(share.clone(), ShareMergeMode::Overwrite) + .await; + + assert!( + result.is_ok(), + "Overwrite mode should succeed with conflicts" + ); + + // Verify the mapping was overwritten + let updated_target_sheet = vault.sheet(&target_sheet_name).await?; + let mapping = updated_target_sheet.mapping().get(&conflicting_path); + assert!(mapping.is_some()); + assert_eq!(mapping.unwrap().id, source_file_id); // Should be source's ID, not target's + + // Clean up + vault.remove_member_from_vault(&sharer_id)?; + vault.remove_member_from_vault(&target_member_id)?; + + Ok(()) +} + +#[tokio::test] +async fn test_share_skip_mode() -> Result<(), std::io::Error> { + let dir = get_test_dir("share_skip_mode").await?; + + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await?; + + // Get vault + let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; + let Some(vault) = Vault::init(config, &dir) else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); + }; + + // Add members + let sharer_id: MemberId = "sharer".to_string(); + let target_member_id: MemberId = "target".to_string(); + + vault + .register_member_to_vault(Member::new(&sharer_id)) + .await?; + vault + .register_member_to_vault(Member::new(&target_member_id)) + .await?; + + // Create source and target sheets + let source_sheet_name: SheetName = "source".to_string(); + let target_sheet_name: SheetName = "target".to_string(); + + let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; + let _target_sheet = vault + .create_sheet(&target_sheet_name, &target_member_id) + .await?; + + // Add mappings to both sheets + let mut source_sheet = vault.sheet(&source_sheet_name).await?; + let mut target_sheet_mut = vault.sheet(&target_sheet_name).await?; + + let conflicting_path = SheetPathBuf::from("src/conflicting.rs"); + let non_conflicting_path = SheetPathBuf::from("src/non_conflicting.rs"); + + let source_file_id = VirtualFileId::from("source_file_id_2"); + let target_file_id = VirtualFileId::from("target_file_id_2"); + let non_conflicting_id = VirtualFileId::from("non_conflicting_id_1"); + + // Add conflicting mapping to both sheets + source_sheet + .add_mapping( + conflicting_path.clone(), + source_file_id.clone(), + "1.0.0".to_string(), + ) + .await?; + + target_sheet_mut + .add_mapping( + conflicting_path.clone(), + target_file_id.clone(), + "1.0.0".to_string(), + ) + .await?; + + // Add non-conflicting mapping only to source + source_sheet + .add_mapping( + non_conflicting_path.clone(), + non_conflicting_id.clone(), + "1.0.0".to_string(), + ) + .await?; + + source_sheet.persist().await?; + target_sheet_mut.persist().await?; + + // Share both mappings + // Need to get the sheet again after persist + let source_sheet = vault.sheet(&source_sheet_name).await?; + source_sheet + .share_mappings( + &target_sheet_name, + vec![conflicting_path.clone(), non_conflicting_path.clone()], + &sharer_id, + "Mixed share".to_string(), + ) + .await?; + + // Get the share + let target_sheet = vault.sheet(&target_sheet_name).await?; + let shares = target_sheet.get_shares().await?; + assert_eq!(shares.len(), 1); + let share = shares[0].clone(); + + // Test 7: Skip mode merge with conflict (should skip conflicting, add non-conflicting) + let result = target_sheet + .merge_share(share.clone(), ShareMergeMode::Skip) + .await; + + assert!(result.is_ok(), "Skip mode should succeed"); + + // Verify only non-conflicting mapping was added + let updated_target_sheet = vault.sheet(&target_sheet_name).await?; + + // Conflicting mapping should still have target's ID + let conflicting_mapping = updated_target_sheet.mapping().get(&conflicting_path); + assert!(conflicting_mapping.is_some()); + assert_eq!(conflicting_mapping.unwrap().id, target_file_id); + + // Non-conflicting mapping should be added + let non_conflicting_mapping = updated_target_sheet.mapping().get(&non_conflicting_path); + assert!(non_conflicting_mapping.is_some()); + assert_eq!(non_conflicting_mapping.unwrap().id, non_conflicting_id); + + // Clean up + vault.remove_member_from_vault(&sharer_id)?; + vault.remove_member_from_vault(&target_member_id)?; + + Ok(()) +} + +#[tokio::test] +async fn test_share_removal() -> Result<(), std::io::Error> { + let dir = get_test_dir("share_removal").await?; + + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await?; + + // Get vault + let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; + let Some(vault) = Vault::init(config, &dir) else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); + }; + + // Add members + let sharer_id: MemberId = "sharer".to_string(); + let target_member_id: MemberId = "target".to_string(); + + vault + .register_member_to_vault(Member::new(&sharer_id)) + .await?; + vault + .register_member_to_vault(Member::new(&target_member_id)) + .await?; + + // Create source and target sheets + let source_sheet_name: SheetName = "source".to_string(); + let target_sheet_name: SheetName = "target".to_string(); + + let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; + let _target_sheet = vault + .create_sheet(&target_sheet_name, &target_member_id) + .await?; + + // Add mapping to source sheet + let mut source_sheet = vault.sheet(&source_sheet_name).await?; + + let file_path = SheetPathBuf::from("src/file.rs"); + let file_id = VirtualFileId::from("file_id_1"); + + source_sheet + .add_mapping(file_path.clone(), file_id.clone(), "1.0.0".to_string()) + .await?; + + source_sheet.persist().await?; + + // Need to get the sheet again after persist + let source_sheet = vault.sheet(&source_sheet_name).await?; + // Share mapping + source_sheet + .share_mappings( + &target_sheet_name, + vec![file_path.clone()], + &sharer_id, + "Test share for removal".to_string(), + ) + .await?; + + // Get the share + let target_sheet = vault.sheet(&target_sheet_name).await?; + let shares = target_sheet.get_shares().await?; + assert_eq!(shares.len(), 1); + let share = shares[0].clone(); + + // Test 8: Remove share + let result = share.remove().await; + + // Check if removal succeeded or failed gracefully + match result { + Ok(_) => { + // Share was successfully removed + let shares_after_removal = target_sheet.get_shares().await?; + assert_eq!(shares_after_removal.len(), 0); + } + Err((returned_share, _error)) => { + // Share removal failed, but we got the share backZ + // Error message may vary, just check that we got an error + // The share should be returned in the error + assert_eq!(returned_share.sharer, sharer_id); + } + } + + // Clean up + vault.remove_member_from_vault(&sharer_id)?; + vault.remove_member_from_vault(&target_member_id)?; + + Ok(()) +} + +#[tokio::test] +async fn test_share_error_conditions() -> Result<(), std::io::Error> { + let dir = get_test_dir("share_errors").await?; + + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await?; + + // Get vault + let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; + let Some(vault) = Vault::init(config, &dir) else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); + }; + + // Add member + let sharer_id: MemberId = "sharer".to_string(); + vault + .register_member_to_vault(Member::new(&sharer_id)) + .await?; + + // Create source sheet + let source_sheet_name: SheetName = "source".to_string(); + let _source_sheet = vault.create_sheet(&source_sheet_name, &sharer_id).await?; + + // Add mapping to source sheet + let mut source_sheet = vault.sheet(&source_sheet_name).await?; + + let file_path = SheetPathBuf::from("src/file.rs"); + let file_id = VirtualFileId::from("file_id_2"); + + source_sheet + .add_mapping(file_path.clone(), file_id.clone(), "1.0.0".to_string()) + .await?; + + source_sheet.persist().await?; + + // Test 9: Share to non-existent sheet should fail + let non_existent_sheet: SheetName = "non_existent".to_string(); + // Need to get the sheet again after persist + let source_sheet = vault.sheet(&source_sheet_name).await?; + let result = source_sheet + .share_mappings( + &non_existent_sheet, + vec![file_path.clone()], + &sharer_id, + "Test".to_string(), + ) + .await; + + assert!(result.is_err()); + + // Test 10: Share non-existent mapping should fail + let target_sheet_name: SheetName = "target".to_string(); + let _target_sheet = vault.create_sheet(&target_sheet_name, &sharer_id).await?; + + let non_existent_path = SheetPathBuf::from("src/non_existent.rs"); + let result = source_sheet + .share_mappings( + &target_sheet_name, + vec![non_existent_path], + &sharer_id, + "Test".to_string(), + ) + .await; + + assert!(result.is_err()); + + // Test 11: Merge non-existent share should fail + let target_sheet = vault.sheet(&target_sheet_name).await?; + let non_existent_share_id: SheetShareId = "non_existent_share".to_string(); + let result = target_sheet + .merge_share_by_id(&non_existent_share_id, ShareMergeMode::Safe) + .await; + + assert!(result.is_err()); + + // Clean up + vault.remove_member_from_vault(&sharer_id)?; + + Ok(()) +} + +#[tokio::test] +async fn test_share_id_generation() -> Result<(), std::io::Error> { + // Test 12: Share ID generation + let sharer_id: MemberId = "test_sharer".to_string(); + + // Generate multiple IDs to ensure they're different + let id1 = Share::gen_share_id(&sharer_id); + let id2 = Share::gen_share_id(&sharer_id); + let id3 = Share::gen_share_id(&sharer_id); + + // IDs should be different due to random component + assert_ne!(id1, id2); + assert_ne!(id1, id3); + assert_ne!(id2, id3); + + // IDs should start with sharer name + assert!(id1.starts_with(&format!("test_sharer@"))); + assert!(id2.starts_with(&format!("test_sharer@"))); + assert!(id3.starts_with(&format!("test_sharer@"))); + + Ok(()) +} diff --git a/legacy_data/tests/src/test_vault_setup_and_member_register.rs b/legacy_data/tests/src/test_vault_setup_and_member_register.rs new file mode 100644 index 0000000..d34e48e --- /dev/null +++ b/legacy_data/tests/src/test_vault_setup_and_member_register.rs @@ -0,0 +1,67 @@ +use std::io::Error; + +use cfg_file::config::ConfigFile; +use vcs_data::{ + constants::{ + SERVER_FILE_MEMBER_INFO, SERVER_FILE_README, SERVER_FILE_VAULT, SERVER_PATH_MEMBER_PUB, + SERVER_PATH_MEMBERS, SERVER_PATH_SHEETS, SERVER_PATH_VF_ROOT, + }, + data::{ + member::Member, + vault::{Vault, vault_config::VaultConfig}, + }, +}; + +use crate::get_test_dir; + +#[tokio::test] +async fn test_vault_setup_and_member_register() -> Result<(), std::io::Error> { + let dir = get_test_dir("member_register").await?; + + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await?; + + // Check if the following files and directories are created in `dir`: + // Files: SERVER_FILE_VAULT, SERVER_FILE_README + // Directories: SERVER_PATH_SHEETS, + // SERVER_PATH_MEMBERS, + // SERVER_PATH_MEMBER_PUB, + // SERVER_PATH_VIRTUAL_FILE_ROOT + assert!(dir.join(SERVER_FILE_VAULT).exists()); + assert!(dir.join(SERVER_FILE_README).exists()); + assert!(dir.join(SERVER_PATH_SHEETS).exists()); + assert!(dir.join(SERVER_PATH_MEMBERS).exists()); + assert!(dir.join(SERVER_PATH_MEMBER_PUB).exists()); + assert!(dir.join(SERVER_PATH_VF_ROOT).exists()); + + // Get vault + let config = VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)).await?; + let Some(vault) = Vault::init(config, &dir) else { + return Err(Error::new(std::io::ErrorKind::NotFound, "Vault not found!")); + }; + + // Add member + let member_id = "test_member"; + vault + .register_member_to_vault(Member::new(member_id)) + .await?; + + const ID_PARAM: &str = "{member_id}"; + + // Check if the member info file exists + assert!( + dir.join(SERVER_FILE_MEMBER_INFO.replace(ID_PARAM, member_id)) + .exists() + ); + + // Remove member + vault.remove_member_from_vault(&member_id.to_string())?; + + // Check if the member info file not exists + assert!( + !dir.join(SERVER_FILE_MEMBER_INFO.replace(ID_PARAM, member_id)) + .exists() + ); + + Ok(()) +} diff --git a/legacy_data/tests/src/test_virtual_file_creation_and_update.rs b/legacy_data/tests/src/test_virtual_file_creation_and_update.rs new file mode 100644 index 0000000..a09f7dc --- /dev/null +++ b/legacy_data/tests/src/test_virtual_file_creation_and_update.rs @@ -0,0 +1,162 @@ +use std::time::Duration; + +use cfg_file::config::ConfigFile; +use tcp_connection_test::{ + handle::{ClientHandle, ServerHandle}, + target::TcpServerTarget, + target_configure::ServerTargetConfig, +}; +use tokio::{ + join, + time::{sleep, timeout}, +}; +use vcs_data::{ + constants::SERVER_FILE_VAULT, + data::{ + member::Member, + vault::{Vault, vault_config::VaultConfig, virtual_file::VirtualFileVersionDescription}, + }, +}; + +use crate::get_test_dir; + +struct VirtualFileCreateClientHandle; +struct VirtualFileCreateServerHandle; + +impl ClientHandle for VirtualFileCreateClientHandle { + async fn process(mut instance: tcp_connection::instance::ConnectionInstance) { + let dir = get_test_dir("virtual_file_creation_and_update_2") + .await + .unwrap(); + // Create first test file for virtual file creation + let test_content_1 = b"Test file content for virtual file creation"; + let temp_file_path_1 = dir.join("test_virtual_file_1.txt"); + + tokio::fs::write(&temp_file_path_1, test_content_1) + .await + .unwrap(); + + // Send the first file to server for virtual file creation + instance.write_file(&temp_file_path_1).await.unwrap(); + + // Create second test file for virtual file update + let test_content_2 = b"Updated test file content for virtual file"; + let temp_file_path_2 = dir.join("test_virtual_file_2.txt"); + + tokio::fs::write(&temp_file_path_2, test_content_2) + .await + .unwrap(); + + // Send the second file to server for virtual file update + instance.write_file(&temp_file_path_2).await.unwrap(); + } +} + +impl ServerHandle for VirtualFileCreateServerHandle { + async fn process(mut instance: tcp_connection::instance::ConnectionInstance) { + let dir = get_test_dir("virtual_file_creation_and_update") + .await + .unwrap(); + + // Setup vault + Vault::setup_vault(dir.clone(), "TestVault").await.unwrap(); + + // Read vault + let Some(vault) = Vault::init( + VaultConfig::read_from(dir.join(SERVER_FILE_VAULT)) + .await + .unwrap(), + &dir, + ) else { + panic!("No vault found!"); + }; + + // Register member + let member_id = "test_member"; + vault + .register_member_to_vault(Member::new(member_id)) + .await + .unwrap(); + + // Create visual file + let virtual_file_id = vault + .create_virtual_file_from_connection(&mut instance, &member_id.to_string()) + .await + .unwrap(); + + // Grant edit right to member + vault + .grant_virtual_file_edit_right(&member_id.to_string(), &virtual_file_id) + .await + .unwrap(); + + // Update visual file + vault + .update_virtual_file_from_connection( + &mut instance, + &member_id.to_string(), + &virtual_file_id, + &"2".to_string(), + VirtualFileVersionDescription { + creator: member_id.to_string(), + description: "Update".to_string(), + }, + ) + .await + .unwrap(); + } +} + +#[tokio::test] +async fn test_virtual_file_creation_and_update() -> Result<(), std::io::Error> { + let host = "localhost:5009"; + + // Server setup + let Ok(server_target) = TcpServerTarget::< + VirtualFileCreateClientHandle, + VirtualFileCreateServerHandle, + >::from_domain(host) + .await + else { + panic!("Test target built failed from a domain named `{}`", host); + }; + + // Client setup + let Ok(client_target) = TcpServerTarget::< + VirtualFileCreateClientHandle, + VirtualFileCreateServerHandle, + >::from_domain(host) + .await + else { + panic!("Test target built failed from a domain named `{}`", host); + }; + + let future_server = async move { + // Only process once + let configured_server = server_target.server_cfg(ServerTargetConfig::default().once()); + + // Listen here + let _ = configured_server.listen().await; + }; + + let future_client = async move { + // Wait for server start + let _ = sleep(Duration::from_secs_f32(1.5)).await; + + // Connect here + let _ = client_target.connect().await; + }; + + let test_timeout = Duration::from_secs(15); + + timeout(test_timeout, async { join!(future_client, future_server) }) + .await + .map_err(|_| { + std::io::Error::new( + std::io::ErrorKind::TimedOut, + format!("Test timed out after {:?}", test_timeout), + ) + })?; + + Ok(()) +} diff --git a/scripts/dev/setup.ps1 b/scripts/dev/setup.ps1 new file mode 100644 index 0000000..cdea5f9 --- /dev/null +++ b/scripts/dev/setup.ps1 @@ -0,0 +1,22 @@ +# Set location to script directory +$scriptPath = $MyInvocation.MyCommand.Path +$scriptDir = Split-Path $scriptPath -Parent +Set-Location (Join-Path $scriptDir "..\..") + +# Setup documents repo +if (Test-Path "docs/Documents/.git") { + Push-Location "docs/Documents" + git fetch origin main + git reset --hard origin/main + Pop-Location +} else { + git clone https://github.com/JustEnoughVCS/Documents.git docs/Documents +} + +# Hide .cargo, .github and .temp directories before build +if (Test-Path .cargo) { + attrib +h .cargo +} +if (Test-Path .temp) { + attrib +h .temp +} diff --git a/scripts/dev/setup.sh b/scripts/dev/setup.sh new file mode 100644 index 0000000..5c7973e --- /dev/null +++ b/scripts/dev/setup.sh @@ -0,0 +1,12 @@ +# Change to the directory where the script is located +cd "$(dirname "$0")/../../" || exit 1 + +# Setup documents repo +if [ -d "docs/Documents/.git" ]; then + cd docs/Documents + git fetch origin main + git reset --hard origin/main + cd - +else + git clone https://github.com/JustEnoughVCS/Documents.git docs/Documents +fi diff --git a/setup.ps1 b/setup.ps1 deleted file mode 100644 index 93ea482..0000000 --- a/setup.ps1 +++ /dev/null @@ -1,17 +0,0 @@ -# Setup documents repo -if (Test-Path "docs/Documents/.git") { - Push-Location "docs/Documents" - git fetch origin main - git reset --hard origin/main - Pop-Location -} else { - git clone https://github.com/JustEnoughVCS/Documents.git docs/Documents -} - -# Hide .cargo, .github and .temp directories before build -if (Test-Path .cargo) { - attrib +h .cargo -} -if (Test-Path .temp) { - attrib +h .temp -} diff --git a/setup.sh b/setup.sh deleted file mode 100755 index fb2a6d6..0000000 --- a/setup.sh +++ /dev/null @@ -1,9 +0,0 @@ -# Setup documents repo -if [ -d "docs/Documents/.git" ]; then - cd docs/Documents - git fetch origin main - git reset --hard origin/main - cd - -else - git clone https://github.com/JustEnoughVCS/Documents.git docs/Documents -fi diff --git a/src/lib.rs b/src/lib.rs index 361fb8c..4574d92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,9 @@ /// Data pub mod data; -// Feature `vcs` -#[cfg(feature = "vcs")] -pub mod vcs { +// Feature `lib` +#[cfg(feature = "lib")] +pub mod lib { pub extern crate vcs_data; pub use vcs_data::*; -- cgit