diff options
Diffstat (limited to 'data')
33 files changed, 5892 insertions, 0 deletions
diff --git a/data/Cargo.toml b/data/Cargo.toml new file mode 100644 index 0000000..7506814 --- /dev/null +++ b/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/data/src/constants.rs b/data/src/constants.rs new file mode 100644 index 0000000..3d839a6 --- /dev/null +++ b/data/src/constants.rs @@ -0,0 +1,118 @@ +// ------------------------------------------------------------------------------------- + +// Project +pub const PATH_TEMP: &str = "./.temp/"; + +// Default Port +pub const PORT: u16 = 25331; + +// Vault Host Name +pub const VAULT_HOST_NAME: &str = "host"; + +// ------------------------------------------------------------------------------------- + +// 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"; + +// ------------------------------------------------------------------------------------- + +// 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/current.rs b/data/src/current.rs new file mode 100644 index 0000000..209c0cc --- /dev/null +++ b/data/src/current.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<PathBuf> { + 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<PathBuf> { + 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<PathBuf>) -> Option<PathBuf> { + 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<PathBuf>) -> Option<PathBuf> { + 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<PathBuf> { + dirs::config_local_dir().map(|path| { + if cfg!(target_os = "linux") { + path.join("jvcs") + } else { + path.join("JustEnoughVCS") + } + }) +} diff --git a/data/src/data.rs b/data/src/data.rs new file mode 100644 index 0000000..ed9383a --- /dev/null +++ b/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/data/src/data/local.rs b/data/src/data/local.rs new file mode 100644 index 0000000..67f3943 --- /dev/null +++ b/data/src/data/local.rs @@ -0,0 +1,269 @@ +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, + }, + current::{current_local_path, find_local_path}, + data::{ + local::{ + config::LocalConfig, + local_sheet::{LocalSheet, LocalSheetData, LocalSheetPathBuf}, + }, + member::MemberId, + sheet::SheetName, + }, +}; + +pub mod align; +pub mod cached_sheet; +pub mod config; +pub mod latest_file_data; +pub mod latest_info; +pub mod local_files; +pub mod local_sheet; +pub mod vault_modified; +pub mod workspace_analyzer; + +const SHEET_NAME: &str = "{sheet_name}"; +const ACCOUNT_NAME: &str = "{account}"; + +pub struct LocalWorkspace { + config: Arc<Mutex<LocalConfig>>, + 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<PathBuf>) -> Option<Self> { + 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<Self> { + 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<PathBuf>, + ) -> 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<Mutex<LocalConfig>> { + 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(ACCOUNT_NAME, member) + .replace(SHEET_NAME, sheet), + ) + } + + /// Read or initialize a local sheet. + pub async fn local_sheet( + &self, + member: &MemberId, + sheet: &SheetName, + ) -> Result<LocalSheet<'_>, 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<Vec<LocalSheetPathBuf>, 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<LocalSheetPathBuf>, + ) -> 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<u16> = 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.rs b/data/src/data/local/align.rs new file mode 100644 index 0000000..b72804c --- /dev/null +++ b/data/src/data/local/align.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<PathBuf>, + prefix: impl Into<String>, +) -> 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<String, (PathBuf, PathBuf)>, + prefix: impl Into<String>, +) -> 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 new file mode 100644 index 0000000..39f9814 --- /dev/null +++ b/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, + }, + current::current_local_path, + data::sheet::{SheetData, SheetName}, +}; + +pub type CachedSheetPathBuf = PathBuf; + +const SHEET_NAME: &str = "{sheet_name}"; +const ACCOUNT_NAME: &str = "{account}"; + +/// # 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<SheetData, std::io::Error> { + 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<PathBuf> { + let current_workspace = current_local_path()?; + Some( + current_workspace + .join(CLIENT_FILE_CACHED_SHEET.replace(SHEET_NAME, &sheet_name.to_string())), + ) + } + + /// Get all cached sheet names + pub async fn cached_sheet_names() -> Result<Vec<SheetName>, 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<Vec<CachedSheetPathBuf>, 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/config.rs b/data/src/data/local/config.rs new file mode 100644 index 0000000..8a89c20 --- /dev/null +++ b/data/src/data/local/config.rs @@ -0,0 +1,375 @@ +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::PORT; +use crate::current::current_local_path; +use crate::data::local::latest_info::LatestInfo; +use crate::data::member::MemberId; +use crate::data::sheet::SheetName; +use crate::data::vault::config::VaultUuid; + +const ACCOUNT: &str = "{account}"; +const SHEET_NAME: &str = "{sheet_name}"; + +#[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<VaultUuid>, + + /// The name of the sheet currently in use. + #[serde(rename = "use")] + sheet_in_use: Option<SheetName>, +} + +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<PathBuf, std::io::Error> { + 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::<Result<Vec<_>, _>>() + .map_err(std::io::Error::other)?; + + let mut moved_items: Vec<MovedItem> = 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::<Result<Vec<_>, _>>() + .map_err(std::io::Error::other)?; + + let mut moved_items: Vec<MovedItem> = 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<VaultUuid> { + 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<SheetName> { + &self.sheet_in_use + } + + /// Get draft folder + pub fn draft_folder( + &self, + account: &MemberId, + sheet_name: &SheetName, + local_workspace_path: impl Into<PathBuf>, + ) -> 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(ACCOUNT, &account_str) + .replace(SHEET_NAME, &sheet_name_str); + local_workspace_path.into().join(draft_path) + } + + /// Get current draft folder + pub fn current_draft_folder(&self) -> Option<PathBuf> { + 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/local/latest_file_data.rs b/data/src/data/local/latest_file_data.rs new file mode 100644 index 0000000..21c647c --- /dev/null +++ b/data/src/data/local/latest_file_data.rs @@ -0,0 +1,105 @@ +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}, + current::current_local_path, + data::{ + member::MemberId, + vault::virtual_file::{VirtualFileId, VirtualFileVersion, VirtualFileVersionDescription}, + }, +}; + +const ACCOUNT: &str = "{account}"; + +/// # 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<VirtualFileId, HeldStatus>, + + /// File version + #[serde(rename = "ver")] + versions: HashMap<VirtualFileId, VirtualFileVersion>, + + /// File histories and descriptions + #[serde(rename = "his")] + histories: HashMap<VirtualFileId, Vec<(VirtualFileVersion, VirtualFileVersionDescription)>>, +} + +#[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<PathBuf, std::io::Error> { + 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(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<MemberId>, + 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 new file mode 100644 index 0000000..e11836b --- /dev/null +++ b/data/src/data/local/latest_info.rs @@ -0,0 +1,83 @@ +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}, + data::{ + member::{Member, MemberId}, + sheet::{SheetData, SheetName, SheetPathBuf}, + vault::{ + sheet_share::{Share, SheetShareId}, + virtual_file::VirtualFileId, + }, + }, +}; + +const ACCOUNT: &str = "{account}"; + +/// # 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<SheetName>, + + /// Invisible sheets, + /// indicating which sheets I can export files to (these sheets are not readable to me) + #[serde(rename = "others")] + pub invisible_sheets: Vec<SheetInfo>, + + /// 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<SheetName>, + + /// 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<VirtualFileId, SheetPathBuf>, + + /// Shares in my sheets, indicating which external merge requests have entries that I can view + #[serde(rename = "shares")] + pub shares_in_my_sheets: HashMap<SheetName, HashMap<SheetShareId, Share>>, + + /// Update instant + #[serde(rename = "update")] + pub update_instant: Option<SystemTime>, + + // Members + /// All member information of the vault, allowing me to contact them more conveniently + #[serde(rename = "members")] + pub vault_members: Vec<Member>, +} + +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(ACCOUNT, member_id)) + } +} + +#[derive(Default, Serialize, Deserialize)] +pub struct SheetInfo { + #[serde(rename = "name")] + pub sheet_name: SheetName, + + #[serde(rename = "holder")] + pub holder_name: Option<MemberId>, +} diff --git a/data/src/data/local/local_files.rs b/data/src/data/local/local_files.rs new file mode 100644 index 0000000..9cc244f --- /dev/null +++ b/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<PathBuf>, +} + +impl IntoIterator for RelativeFiles { + type Item = PathBuf; + type IntoIter = std::vec::IntoIter<Self::Item>; + + 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<RelativeFiles> { + // Get Relative Paths + let Ok(paths) = format_input_paths_and_ignore_outside_paths(local_path, paths).await else { + return None; + }; + let files: Vec<PathBuf> = 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<Vec<PathBuf>, 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<Vec<PathBuf>, std::io::Error> { + let result = format_input_paths(local_path, files).await?; + let result: Vec<PathBuf> = 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<PathBuf>, +) -> Result<Vec<PathBuf>, std::io::Error> { + let result: Result<Vec<PathBuf>, _> = 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<PathBuf>) -> Vec<PathBuf> { + 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 new file mode 100644 index 0000000..6f9924c --- /dev/null +++ b/data/src/data/local/local_sheet.rs @@ -0,0 +1,377 @@ +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, +} + +#[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<LocalFilePathBuf, LocalMappingMetadata>, + + #[serde(rename = "vfs")] + pub(crate) vfs: HashMap<VirtualFileId, LocalFilePathBuf>, +} + +#[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<String>, +} + +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<String> { + &self.last_modify_check_hash + } + + /// Setter for last_modifiy_check_hash + pub fn set_last_modifiy_check_hash(&mut self, hash: Option<String>) { + 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<S>(instant: &Instant, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_u64(instant.elapsed().as_secs()) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result<Instant, D::Error> + 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<'a> LocalSheet<'a> { + /// 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.data.mapping.contains_key(&path) + || self.data.vfs.contains_key(&mapping.mapping_vfid) + { + return Err(Error::new( + std::io::ErrorKind::AlreadyExists, + "Mapping already exists", + )); + } + + self.data.mapping.insert(path, mapping); + 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.data.mapping.contains_key(&to) { + return Err(Error::new( + std::io::ErrorKind::AlreadyExists, + "To path already exists.", + )); + } + + let Some(old_value) = self.data.mapping.remove(&from) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "From path is not found.", + )); + }; + + self.data.mapping.insert(to, old_value); + + Ok(()) + } + + /// Remove mapping from local sheet + pub fn remove_mapping( + &mut self, + path: &LocalFilePathBuf, + ) -> Result<LocalMappingMetadata, std::io::Error> { + let path = format_path(path)?; + match self.data.mapping.remove(&path) { + Some(mapping) => 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.data.mapping.get(&path) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Path is not found.", + )); + }; + Ok(data) + } + + /// Get muttable 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.data.mapping.get_mut(&path) else { + return Err(Error::new( + std::io::ErrorKind::NotFound, + "Path is not found.", + )); + }; + Ok(data) + } + + /// 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<PathBuf>) -> Result<(), std::io::Error> { + let path = path.into(); + + self.data.vfs = HashMap::new(); + for (path, mapping) in self.data.mapping.iter() { + self.data + .vfs + .insert(mapping.mapping_vfid.clone(), path.clone()); + } + + LocalSheetData::write_to(&self.data, path).await?; + Ok(()) + } + + /// Get path by VirtualFileId + pub fn path_by_id(&self, vfid: &VirtualFileId) -> Option<&PathBuf> { + self.data.vfs.get(vfid) + } +} diff --git a/data/src/data/local/vault_modified.rs b/data/src/data/local/vault_modified.rs new file mode 100644 index 0000000..563d11f --- /dev/null +++ b/data/src/data/local/vault_modified.rs @@ -0,0 +1,30 @@ +use crate::{constants::CLIENT_FILE_VAULT_MODIFIED, current::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 new file mode 100644 index 0000000..f2d83ff --- /dev/null +++ b/data/src/data/local/workspace_analyzer.rs @@ -0,0 +1,327 @@ +use std::{ + collections::{HashMap, HashSet}, + io::Error, + path::PathBuf, +}; + +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<VirtualFileId, (FromRelativePathBuf, ToRelativePathBuf)>, + + /// Newly created local files + pub created: HashSet<CreatedRelativePathBuf>, + + /// Lost local files + pub lost: HashSet<LostRelativePathBuf>, + + /// Erased local files + pub erased: HashSet<LostRelativePathBuf>, + + /// 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<ModifiedRelativePathBuf>, +} + +struct AnalyzeContext<'a> { + member: MemberId, + sheet_name: SheetName, + local_sheet: Option<LocalSheet<'a>>, + cached_sheet_data: Option<SheetData>, +} + +impl<'a> AnalyzeResult<'a> { + /// Analyze all files, calculate the file information provided + pub async fn analyze_local_status( + local_workspace: &'a LocalWorkspace, + ) -> Result<AnalyzeResult<'a>, 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<PathBuf>, + 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<PathBuf> = 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<PathBuf> = new_files + .iter() + .map(|p| workspace.local_path.join(p)) + .collect(); + let file_hashes: HashSet<(PathBuf, String)> = + match calc_sha1_multi::<PathBuf, Vec<PathBuf>>(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<String, FromRelativePathBuf> = + 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<PathBuf>, + 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/member.rs b/data/src/data/member.rs new file mode 100644 index 0000000..7e99488 --- /dev/null +++ b/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<String, String>, +} + +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<str> for Member { + fn as_ref(&self) -> &str { + &self.id + } +} + +impl Member { + /// Create member struct by id + pub fn new(new_id: impl Into<String>) -> 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<String>) -> Option<&String> { + self.metadata.get(&key.into()) + } + + /// Set metadata + pub fn set_metadata( + &mut self, + key: impl AsRef<str>, + value: impl Into<String>, + ) -> Option<String> { + self.metadata.insert(key.as_ref().to_string(), value.into()) + } +} diff --git a/data/src/data/sheet.rs b/data/src/data/sheet.rs new file mode 100644 index 0000000..64b1985 --- /dev/null +++ b/data/src/data/sheet.rs @@ -0,0 +1,280 @@ +use std::{collections::HashMap, path::PathBuf}; + +use cfg_file::{ConfigFile, config::ConfigFile}; +use serde::{Deserialize, Serialize}; + +use crate::{ + constants::SERVER_FILE_SHEET, + data::{ + member::MemberId, + vault::{ + Vault, + virtual_file::{VirtualFileId, VirtualFileVersion}, + }, + }, +}; + +pub type SheetName = String; +pub type SheetPathBuf = PathBuf; + +const SHEET_NAME: &str = "{sheet_name}"; + +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<MemberId>, + + /// Mapping of sheet paths to virtual file IDs + #[serde(rename = "map")] + pub(crate) mapping: HashMap<SheetPathBuf, SheetMappingMetadata>, + + /// Mapping of virtual file Ids to sheet paths + #[serde(rename = "id_map")] + pub(crate) id_mapping: Option<HashMap<VirtualFileId, SheetPathBuf>>, +} + +#[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<SheetPathBuf, SheetMappingMetadata> { + &self.data.mapping + } + + /// Get the muttable mapping of this sheet + pub fn mapping_mut(&mut self) -> &mut HashMap<SheetPathBuf, SheetMappingMetadata> { + &mut self.data.mapping + } + + /// Get the id_mapping of this sheet data + pub fn id_mapping(&self) -> &Option<HashMap<VirtualFileId, SheetPathBuf>> { + &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<SheetMappingMetadata> { + 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<str>) -> PathBuf { + vault + .vault_path() + .join(SERVER_FILE_SHEET.replace(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<SheetPathBuf, SheetMappingMetadata> { + &self.mapping + } + + /// Get the muttable mapping of this sheet data + pub fn mapping_mut(&mut self) -> &mut HashMap<SheetPathBuf, SheetMappingMetadata> { + &mut self.mapping + } + + /// Get the id_mapping of this sheet data + pub fn id_mapping(&self) -> &Option<HashMap<VirtualFileId, SheetPathBuf>> { + &self.id_mapping + } + + /// Get the muttable id_mapping of this sheet data + pub fn id_mapping_mut(&mut self) -> &mut Option<HashMap<VirtualFileId, SheetPathBuf>> { + &mut self.id_mapping + } +} diff --git a/data/src/data/user.rs b/data/src/data/user.rs new file mode 100644 index 0000000..9f52fdc --- /dev/null +++ b/data/src/data/user.rs @@ -0,0 +1,28 @@ +use crate::current::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<Self> { + 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<P: Into<PathBuf>>(path: P) -> Option<Self> { + 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 new file mode 100644 index 0000000..d77bc02 --- /dev/null +++ b/data/src/data/user/accounts.rs @@ -0,0 +1,164 @@ +use std::{ + fs, + io::{Error, ErrorKind}, + path::PathBuf, +}; + +use cfg_file::config::ConfigFile; + +use crate::{ + constants::{USER_FILE_ACCOUNTS, USER_FILE_KEY, USER_FILE_MEMBER}, + data::{ + member::{Member, MemberId}, + user::UserDirectory, + }, +}; + +const SELF_ID: &str = "{self_id}"; + +/// Account Management +impl UserDirectory { + /// Read account from configuration file + pub async fn account(&self, id: &MemberId) -> Result<Member, std::io::Error> { + 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<Vec<MemberId>, std::io::Error> { + let accounts_path = self + .local_path + .join(USER_FILE_ACCOUNTS.replace(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<Vec<Member>, 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(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<PathBuf> { + 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<PathBuf> { + 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(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(SELF_ID, id.to_string().as_str())) + } +} diff --git a/data/src/data/vault.rs b/data/src/data/vault.rs new file mode 100644 index 0000000..595997a --- /dev/null +++ b/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, + }, + current::{current_vault_path, find_vault_path}, + data::{member::Member, vault::config::VaultConfig}, +}; + +pub mod config; +pub mod member; +pub mod service; +pub mod sheet_share; +pub mod sheets; +pub mod virtual_file; + +pub struct Vault { + config: Arc<VaultConfig>, + 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<PathBuf>) -> Option<Self> { + 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<Self> { + 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<PathBuf>, + vault_name: impl AsRef<str>, + ) -> 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<str>, + ) -> Result<(), std::io::Error> { + Self::setup_vault(current_dir()?, vault_name).await?; + Ok(()) + } + + /// Get vault configuration + pub fn config(&self) -> &Arc<VaultConfig> { + &self.config + } +} diff --git a/data/src/data/vault/config.rs b/data/src/data/vault/config.rs new file mode 100644 index 0000000..caa8552 --- /dev/null +++ b/data/src/data/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<bool> for ServiceEnabled { + fn into(self) -> bool { + match self { + ServiceEnabled::Enable => true, + ServiceEnabled::Disable => false, + } + } +} + +impl Into<bool> 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<MemberId>, + + /// 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<BehaviourEnabled>, + + /// Logger Level + #[serde(rename = "logger_level")] + logger_level: Option<LoggerLevel>, + + /// 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<ServiceEnabled>, // 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<AuthMode>, // 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<String>) { + 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<MemberId> { + &self.vault_host_list + } + + /// Set vault admin list + pub fn set_vault_host_list(&mut self, vault_host_list: Vec<MemberId>) { + 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/member.rs b/data/src/data/vault/member.rs new file mode 100644 index 0000000..9d22d09 --- /dev/null +++ b/data/src/data/vault/member.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<Member, std::io::Error> { + 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<Vec<MemberId>, 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<Vec<Member>, 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<PathBuf> { + 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<PathBuf> { + 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/service.rs b/data/src/data/vault/service.rs new file mode 100644 index 0000000..3f59c30 --- /dev/null +++ b/data/src/data/vault/service.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/data/src/data/vault/sheet_share.rs b/data/src/data/vault/sheet_share.rs new file mode 100644 index 0000000..1e692f1 --- /dev/null +++ b/data/src/data/vault/sheet_share.rs @@ -0,0 +1,424 @@ +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::{ + 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; + +const SHEET_NAME: &str = "{sheet_name}"; +const SHARE_ID: &str = "{share_id}"; + +#[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<PathBuf>, + + /// 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<SheetPathBuf, SheetMappingMetadata>, +} + +#[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<PathBuf>, + + /// Duplicate files exist + pub duplicate_file: Vec<PathBuf>, +} + +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(SHEET_NAME, &sheet_name) + .replace(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<PathBuf> { + let sheet_name = snake_case!(sheet_name.clone()); + let shares_dir = self + .vault_path() + .join(SERVER_PATH_SHARES.replace(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<Vec<Share>, 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<Share, std::io::Error> { + 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<PathBuf>, + sharer: &MemberId, + description: String, + ) -> Result<Share, std::io::Error> { + 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/sheets.rs b/data/src/data/vault/sheets.rs new file mode 100644 index 0000000..c22c849 --- /dev/null +++ b/data/src/data/vault/sheets.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<Vec<Sheet<'a>>, 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<Vec<SheetName>, 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<Sheet<'a>, 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<Sheet<'a>, 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/virtual_file.rs b/data/src/data/vault/virtual_file.rs new file mode 100644 index 0000000..8dbcb5d --- /dev/null +++ b/data/src/data/vault/virtual_file.rs @@ -0,0 +1,506 @@ +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::{ + SERVER_FILE_VF_META, SERVER_FILE_VF_VERSION_INSTANCE, SERVER_PATH_VF_ROOT, + SERVER_PATH_VF_STORAGE, SERVER_PATH_VF_TEMP, + }, + data::{member::MemberId, vault::Vault}, +}; + +pub type VirtualFileId = String; +pub type VirtualFileVersion = String; + +const VF_PREFIX: &str = "vf-"; +const ID_PARAM: &str = "{vf_id}"; +const ID_INDEX: &str = "{vf_index}"; +const VERSION_PARAM: &str = "{vf_version}"; +const TEMP_NAME: &str = "{temp_name}"; + +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<VirtualFileVersion, VirtualFileVersionDescription>, + + /// Histories + #[serde(rename = "histories")] + histories: Vec<VirtualFileVersion>, +} + +#[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(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<PathBuf, std::io::Error> { + Ok(self.vault_path().join( + SERVER_PATH_VF_STORAGE + .replace(ID_PARAM, &id.to_string()) + .replace(ID_INDEX, &Self::vf_index(id)?), + )) + } + + // Generate index path of virtual file + fn vf_index(id: &VirtualFileId) -> Result<String, std::io::Error> { + // 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(ID_PARAM, &id.to_string()) + .replace(ID_INDEX, &Self::vf_index(id).unwrap_or_default()) + .replace(VERSION_PARAM, &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(ID_PARAM, &id.to_string()) + .replace(ID_INDEX, &Self::vf_index(id).unwrap_or_default()), + ) + } + + /// Get the virtual file with the given ID + pub fn virtual_file(&self, id: &VirtualFileId) -> Result<VirtualFile<'_>, 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<VirtualFileMeta, std::io::Error> { + 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<VirtualFileId, std::io::Error> { + const FIRST_VERSION: &str = "0.1.0"; + 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, &FIRST_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::<VirtualFileVersion, VirtualFileVersionDescription>::new(); + version_description.insert( + FIRST_VERSION.to_string(), + VirtualFileVersionDescription { + creator: member_id.clone(), + description: "Track".to_string(), + }, + ); + // Create metadata + let mut meta = VirtualFileMeta { + current_version: FIRST_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(FIRST_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<bool, std::io::Error> { + 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<VirtualFileMeta, std::io::Error> { + self.current_vault.virtual_file_meta(&self.id).await + } +} + +impl VirtualFileMeta { + /// Get all versions of the virtual file + pub fn versions(&self) -> &Vec<VirtualFileVersion> { + &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<i32> { + 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<VirtualFileVersion> { + 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<VirtualFileVersion, VirtualFileVersionDescription> { + &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/lib.rs b/data/src/lib.rs new file mode 100644 index 0000000..1b41391 --- /dev/null +++ b/data/src/lib.rs @@ -0,0 +1,5 @@ +pub mod constants; +pub mod current; + +#[allow(dead_code)] +pub mod data; diff --git a/data/tests/Cargo.toml b/data/tests/Cargo.toml new file mode 100644 index 0000000..e7a0fcc --- /dev/null +++ b/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 = "../../data" } + +# Async & Networking +tokio = { version = "1.48.0", features = ["full"] } diff --git a/data/tests/src/lib.rs b/data/tests/src/lib.rs new file mode 100644 index 0000000..ced2d3d --- /dev/null +++ b/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<PathBuf, std::io::Error> { + 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 new file mode 100644 index 0000000..8fa2676 --- /dev/null +++ b/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, 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 new file mode 100644 index 0000000..6683d06 --- /dev/null +++ b/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, 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 new file mode 100644 index 0000000..89891d6 --- /dev/null +++ b/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, + config::VaultConfig, + sheet_share::{Share, ShareMergeMode, SheetShareId}, + 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 new file mode 100644 index 0000000..286a4a2 --- /dev/null +++ b/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, 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 new file mode 100644 index 0000000..2d9d393 --- /dev/null +++ b/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, config::VaultConfig, virtual_file::VirtualFileVersionDescription}, + }, +}; + +use crate::get_test_dir; + +struct VirtualFileCreateClientHandle; +struct VirtualFileCreateServerHandle; + +impl ClientHandle<VirtualFileCreateServerHandle> 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<VirtualFileCreateClientHandle> 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(()) +} |
