From a50571ead278423d98ed5de25fbbd2ff725f4333 Mon Sep 17 00:00:00 2001 From: Neon Date: Wed, 4 Jun 2025 14:39:04 +0800 Subject: [PATCH 1/6] chore: update dep --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 52411e10e..2a2f4c0db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,8 +84,8 @@ git2 = "0.20.2" home = "0.5.11" ring = "0.17.14" cedar-policy = "4.4.1" -secp256k1 = "0.30.0" -pgp = "0.15.0" +secp256k1 = "0.31.0" +pgp = "0.16.0" openssl = "0.10.73" oauth2 = "5.0.0" base64 = "0.22.1" From 0e6832115cd24039b07e64a8c648f195cd0bff46 Mon Sep 17 00:00:00 2001 From: Neon Date: Wed, 4 Jun 2025 20:28:25 +0800 Subject: [PATCH 2/6] fix: more robust config path for crossing platforms --- Cargo.toml | 1 + common/Cargo.toml | 1 + common/src/config.rs | 74 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2a2f4c0db..104cef137 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -140,6 +140,7 @@ diffs = "0.5.1" libc = "0.2.172" zstd-sys = "2.0.15+zstd.1.5.7" quickcheck = "1.0.3" +directories = "6.0.0" [profile.release] debug = true diff --git a/common/Cargo.toml b/common/Cargo.toml index 1a887ca6f..8d4939835 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -24,3 +24,4 @@ serde_json = { workspace = true } regex = { workspace = true } sea-orm = { workspace = true, features = ["macros"] } utoipa = { workspace = true } +directories = { workspace = true } diff --git a/common/src/config.rs b/common/src/config.rs index 109d4c3e1..f5956ca17 100644 --- a/common/src/config.rs +++ b/common/src/config.rs @@ -11,6 +11,68 @@ use std::rc::Rc; use crate::utils; +/// Retrieves the base directory path for Mega +/// +/// The directory is determined in the following priority order: +/// 1. Uses the `MEGA_BASE_DIR` environment variable if set +/// 2. Falls back to system default paths when environment variable is not set: +/// - On Linux: `~/.local/share/mega` +/// - On Windows: `C:\Users\{UserName}\AppData\Local\mega` +/// - On macOS: `~/Library/Application Support/mega` +/// +/// # Returns +/// A PathBuf containing the base directory path +/// +/// # Panics +/// Will panic if both conditions occur: +/// - Environment variable is not set +/// - System base directories cannot be determined +/// +pub fn mega_base() -> PathBuf { + // Get the base directory from the environment variable or use the default + let base_dir = std::env::var("MEGA_BASE_DIR").unwrap_or_else(|_| { + let base_dirs = directories::BaseDirs::new().unwrap(); + base_dirs + .data_local_dir() + .join("mega") + .to_str() + .unwrap() + .to_string() + }); + PathBuf::from(base_dir) +} + +/// Retrieves the cache directory path for Mega +/// +/// The directory is determined in the following priority order: +/// 1. Uses the `MEGA_CACHE_DIR` environment variable if set +/// 2. Falls back to system default paths when environment variable is not set: +/// - On Linux: `~/.cache/mega` +/// - On Windows: `C:\Users\{username}\AppData\Local\Cache\mega` +/// - On macOS: `~/Library/Caches/mega` +/// +/// # Returns +/// A PathBuf containing the cache directory path +/// +/// # Panics +/// Will panic if both conditions occur: +/// - Environment variable is not set +/// - System cache directories cannot be determined +/// +pub fn mega_cache() -> PathBuf { + // Get the cache directory from the environment variable or use the default + let cache_dir = std::env::var("MEGA_CACHE_DIR").unwrap_or_else(|_| { + let base_dirs = directories::BaseDirs::new().unwrap(); + base_dirs + .cache_dir() + .join("mega") + .to_str() + .unwrap() + .to_string() + }); + PathBuf::from(cache_dir) +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Config { pub base_dir: PathBuf, @@ -89,9 +151,7 @@ impl Config { impl Default for Config { fn default() -> Self { - let base_dir = PathBuf::from( - std::env::var("MEGA_BASE_DIR").unwrap_or_else(|_| "/tmp/.mega".to_string()), - ); + let base_dir = mega_base(); std::fs::create_dir_all(&base_dir).unwrap(); let bin_name = utils::get_current_bin_name(); @@ -209,7 +269,7 @@ pub struct LogConfig { impl Default for LogConfig { fn default() -> Self { Self { - log_path: PathBuf::from("/tmp/.mega/logs"), + log_path: mega_cache().join("logs"), level: String::from("info"), print_std: true, } @@ -230,7 +290,7 @@ impl Default for DbConfig { fn default() -> Self { Self { db_type: String::from("sqlite"), - db_path: PathBuf::from("/tmp/.mega/mega.db"), + db_path: mega_base().join("mega.db"), db_url: String::from("postgres://mega:mega@localhost:5432/mega"), max_connection: 32, min_connection: 16, @@ -296,7 +356,7 @@ impl Default for PackConfig { Self { pack_decode_mem_size: "4G".to_string(), pack_decode_disk_size: "20%".to_string(), - pack_decode_cache_path: PathBuf::from("/tmp/.mega/cache"), + pack_decode_cache_path: mega_cache().join("pack_decode_cache"), clean_cache_after_decode: true, channel_message_size: 1_000_000, } @@ -446,7 +506,7 @@ pub struct LFSLocalConfig { impl Default for LFSLocalConfig { fn default() -> Self { Self { - lfs_file_path: PathBuf::from("/tmp/.mega/lfs"), + lfs_file_path: mega_base().join("lfs"), enable_split: true, split_size: "20M".to_string(), } From 727df8836d6f8701c42b0cdc1b82f9e1efc11d2a Mon Sep 17 00:00:00 2001 From: Neon Date: Wed, 4 Jun 2025 20:55:51 +0800 Subject: [PATCH 3/6] test: improved coverage for config string test --- Cargo.toml | 4 ++-- common/src/config.rs | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 104cef137..2196c5bbc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,8 +84,8 @@ git2 = "0.20.2" home = "0.5.11" ring = "0.17.14" cedar-policy = "4.4.1" -secp256k1 = "0.31.0" -pgp = "0.16.0" +secp256k1 = "0.30.0" +pgp = "0.15.0" openssl = "0.10.73" oauth2 = "5.0.0" base64 = "0.22.1" diff --git a/common/src/config.rs b/common/src/config.rs index f5956ca17..9d854527d 100644 --- a/common/src/config.rs +++ b/common/src/config.rs @@ -543,6 +543,30 @@ pub struct OauthConfig { #[cfg(test)] mod test { + use super::*; + use std::path::Path; + + fn check_file_permission(path: &Path) { + let metadata = std::fs::metadata(path).expect("Failed to read metadata"); + assert!(!metadata.permissions().readonly(), "File should not be read-only"); + } + + #[test] + fn test_mega_base() { + let base_dir = mega_base(); + std::fs::create_dir_all(&base_dir).expect("Failed to create base directory"); + assert!(base_dir.exists(), "Mega base directory should exist"); + check_file_permission(&base_dir); + } + + #[test] + fn test_mega_cache() { + let cache_dir = mega_cache(); + std::fs::create_dir_all(&cache_dir).expect("Failed to create cache directory"); + assert!(cache_dir.exists(), "Mega cache directory should exist"); + check_file_permission(&cache_dir); + } + #[test] fn test_get_size_from_str() { use crate::config::PackConfig; From 4e2634b29f27c8a16fcc2680dd6a3035cfa22f95 Mon Sep 17 00:00:00 2001 From: Neon Date: Wed, 4 Jun 2025 21:16:57 +0800 Subject: [PATCH 4/6] fix(vault): temporal fix for invalid path in vault initialization --- vault/Cargo.toml | 1 + vault/src/vault.rs | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vault/Cargo.toml b/vault/Cargo.toml index 9a474348d..784f48ac7 100644 --- a/vault/Cargo.toml +++ b/vault/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +common = { workspace = true} rusty_vault = { workspace = true } serde_json = { workspace = true } go-defer = { workspace = true } diff --git a/vault/src/vault.rs b/vault/src/vault.rs index 4f1d4693c..35dc4c237 100644 --- a/vault/src/vault.rs +++ b/vault/src/vault.rs @@ -8,7 +8,6 @@ use serde::{Deserialize, Serialize}; use serde_json::{Map, Value}; use std::collections::HashMap; use std::fs; -use std::path::PathBuf; use std::sync::{Arc, RwLock}; #[derive(Serialize, Deserialize, Debug)] @@ -31,7 +30,7 @@ lazy_static! { /// Initialize the vault core, used in `lazy_static!` fn init() -> CoreInfo { const CORE_KEY_FILE: &str = "core_key.json"; // where the core key is stored, like `root_token` - let dir = PathBuf::from("/tmp/rusty_vault_pki_module"); // RustyVault files TODO configurable + let dir = common::config::mega_base().join("vault"); let core_key_path = dir.join(CORE_KEY_FILE); // let dir = env::temp_dir().join("rusty_vault_pki_module"); // TODO: 改成数据库? From ea8a9271f23d40e9908ef7b512ce1816ebbdb0e6 Mon Sep 17 00:00:00 2001 From: Neon Date: Wed, 4 Jun 2025 21:23:20 +0800 Subject: [PATCH 5/6] chore: cleanup --- common/src/config.rs | 5 ++++- vault/src/pgp.rs | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/common/src/config.rs b/common/src/config.rs index 9d854527d..dc085d6cf 100644 --- a/common/src/config.rs +++ b/common/src/config.rs @@ -548,7 +548,10 @@ mod test { fn check_file_permission(path: &Path) { let metadata = std::fs::metadata(path).expect("Failed to read metadata"); - assert!(!metadata.permissions().readonly(), "File should not be read-only"); + assert!( + !metadata.permissions().readonly(), + "File should not be read-only" + ); } #[test] diff --git a/vault/src/pgp.rs b/vault/src/pgp.rs index 926262986..f9db57bd8 100644 --- a/vault/src/pgp.rs +++ b/vault/src/pgp.rs @@ -4,10 +4,10 @@ /// using asynchronous operations. use smallvec::smallvec; -use pgp::{SecretKeyParams, SecretKeyParamsBuilder, SubkeyParamsBuilder}; -use pgp::types::SecretKeyTrait; pub use pgp::composed::{Deserializable, SignedPublicKey, SignedSecretKey}; +use pgp::types::SecretKeyTrait; pub use pgp::KeyType; +use pgp::{SecretKeyParams, SecretKeyParamsBuilder, SubkeyParamsBuilder}; use crate::vault::{delete_secret, read_secret, write_secret}; From e0d62ea352da305ade4a2991ce3132f3f06572fe Mon Sep 17 00:00:00 2001 From: Neon Date: Wed, 4 Jun 2025 22:03:20 +0800 Subject: [PATCH 6/6] fix: store windows path with double backslash --- common/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/config.rs b/common/src/config.rs index dc085d6cf..b79f66943 100644 --- a/common/src/config.rs +++ b/common/src/config.rs @@ -164,7 +164,7 @@ impl Default for Config { .lines() .map(|line| { if line.starts_with("base_dir ") { - format!("base_dir = \"{}\"", base_dir.to_str().unwrap()) + format!("base_dir = {:?}", base_dir) } else { line.to_string() }