diff --git a/Cargo.lock b/Cargo.lock index c6c96c99a..ae1504ace 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1227,6 +1227,7 @@ dependencies = [ "sha2 0.11.2", "thiserror 2.0.20", "tokio", + "tristr", ] [[package]] diff --git a/crates/clash-verge-runtime-bundle/Cargo.toml b/crates/clash-verge-runtime-bundle/Cargo.toml index 771283eb3..603bb8de0 100644 --- a/crates/clash-verge-runtime-bundle/Cargo.toml +++ b/crates/clash-verge-runtime-bundle/Cargo.toml @@ -10,6 +10,7 @@ hex = "0.4.3" log = { workspace = true } serde_yaml_ng = { workspace = true } sha2 = { workspace = true } +tristr = { workspace = true } tokio = { workspace = true, features = ["fs"] } [lints] diff --git a/crates/clash-verge-runtime-bundle/src/lib.rs b/crates/clash-verge-runtime-bundle/src/lib.rs index ceeb987fc..e1244f7cd 100644 --- a/crates/clash-verge-runtime-bundle/src/lib.rs +++ b/crates/clash-verge-runtime-bundle/src/lib.rs @@ -7,6 +7,7 @@ use serde_yaml_ng::{Mapping, Value}; use sha2::{Digest as _, Sha256}; use std::collections::{BTreeMap, HashSet}; use std::path::{Component, Path, PathBuf, Prefix}; +use tristr::TriStr; pub const GEO_ASSETS: &[&str] = &[ "Country.mmdb", @@ -22,13 +23,13 @@ pub const GEO_ASSETS: &[&str] = &[ pub struct RemoteProviderRef { pub section: &'static str, pub name: String, - pub destination: String, + pub destination: TriStr, pub url: String, } pub struct RuntimeAsset { pub source: String, - pub destination: String, + pub destination: TriStr, } pub struct RuntimeBundlePlan { @@ -77,8 +78,8 @@ struct ProviderPathOwners { pub fn resolve_provider_path_conflicts(config: &mut Mapping, config_root: &Path) -> Result<()> { let config_root = std::fs::canonicalize(config_root)?; - let mut paths = BTreeMap::::new(); - let mut reserved: HashSet = GEO_ASSETS.iter().map(|name| (*name).to_owned()).collect(); + let mut paths = BTreeMap::::new(); + let mut reserved: HashSet = GEO_ASSETS.iter().copied().map(TriStr::from).collect(); for section in ["proxy-providers", "rule-providers"] { let Some(providers) = config.get(section).and_then(Value::as_mapping) else { continue; @@ -98,6 +99,7 @@ pub fn resolve_provider_path_conflicts(config: &mut Mapping, config_root: &Path) let Ok(destination) = destination else { continue; }; + let destination = TriStr::from(destination.as_str()); reserved.insert(destination.clone()); let owners = paths.entry(destination).or_default(); match (is_remote, provider.get("url").and_then(Value::as_str)) { @@ -131,7 +133,7 @@ pub fn resolve_provider_path_conflicts(config: &mut Mapping, config_root: &Path) Ok(()) } -fn allocate_provider_destination(destination: &str, url: &str, reserved: &mut HashSet) -> String { +fn allocate_provider_destination(destination: &str, url: &str, reserved: &mut HashSet) -> String { let mut digest = Sha256::new(); digest.update(destination.as_bytes()); digest.update([0]); @@ -148,7 +150,7 @@ fn allocate_provider_destination(destination: &str, url: &str, reserved: &mut Ha .with_file_name(format!("cvr-{digest}{suffix}{extension}")) .to_string_lossy() .replace('\\', "/"); - if reserved.insert(candidate.clone()) { + if reserved.insert(TriStr::from(candidate.as_str())) { return candidate; } sequence += 1; @@ -181,10 +183,10 @@ fn collect_runtime_parts(mut config: Value, config_root: &Path) -> Result, + destinations: &mut HashSet, assets: &mut Vec, remote_providers: &mut Vec, ) -> Result<()> { @@ -228,11 +230,12 @@ fn collect_provider_assets( (true, Some(url)) => { let destination = provider_destination(config_root, raw_path)?; // Reserve remote destinations too; copied and downloaded files must not collide. - if destinations.insert(destination.clone()) { + let reserved = TriStr::from(destination.as_str()); + if destinations.insert(reserved.clone()) { remote_providers.push(RemoteProviderRef { section, name: name.as_str().unwrap_or_default().to_owned(), - destination: destination.clone(), + destination: reserved, url, }); } else { @@ -265,10 +268,11 @@ fn collect_provider_assets( continue; }; let destination = destination_below_root(config_root, &source)?; - if destinations.insert(destination.clone()) { + let reserved = TriStr::from(destination.as_str()); + if destinations.insert(reserved.clone()) { assets.push(RuntimeAsset { source: source.to_string_lossy().into_owned(), - destination: destination.clone(), + destination: reserved, }); } destination @@ -283,7 +287,7 @@ fn collect_provider_assets( fn collect_ssh_private_keys( config: &mut Value, config_root: &Path, - destinations: &mut HashSet, + destinations: &mut HashSet, assets: &mut Vec, ) { let Some(config) = config.as_mapping_mut() else { @@ -334,7 +338,7 @@ fn collect_ssh_private_keys( let destination = allocate_provider_destination("ssh-private-key", &source, destinations); assets.push(RuntimeAsset { source, - destination: destination.clone(), + destination: TriStr::from(destination.as_str()), }); proxy.insert(Value::String("private-key".to_owned()), Value::String(destination)); } @@ -466,7 +470,10 @@ mod tests { assert_eq!(bundle.remote_providers.len(), 2); for declared in readback { assert!(bundle.remote_providers.contains(&declared)); - assert_eq!(config[declared.section][&declared.name]["path"], declared.destination); + assert_eq!( + config[declared.section][&declared.name]["path"], + declared.destination.as_str() + ); assert_eq!( config[declared.section][&declared.name]["url"], original[declared.section][&declared.name]["url"] diff --git a/src-tauri/src/core/service.rs b/src-tauri/src/core/service.rs index fb5aa9e65..807836c6d 100644 --- a/src-tauri/src/core/service.rs +++ b/src-tauri/src/core/service.rs @@ -619,14 +619,14 @@ async fn collect_service_runtime_bundle(config_file: &Path) -> Result Result { let loaded_before = provider_load_times().await; let mut fetched = Vec::new(); for declared in applied_remote_providers(&runtime, &app_dir)? { - let (temp, file) = match create_sync_temp(&app_dir.join(&declared.destination)).await { + let (temp, file) = match create_sync_temp(&app_dir.join(declared.destination.as_str())).await { Ok(created) => created, Err(error) => { logging!(warn, Type::Service, "{error:#}"); @@ -1137,7 +1137,7 @@ async fn publish_fetched( }; let mut synced = 0; for cache in fetched { - let target = app_dir.join(&cache.declared.destination); + let target = app_dir.join(cache.declared.destination.as_str()); if !declared.contains(&cache.declared) || same_contents(&cache.temp, &target).await { remove_temp(&cache.temp).await; continue; @@ -1214,7 +1214,7 @@ async fn recheck_identity( cache: &FetchedCache, ) -> Result { let request = RuntimeFileRequest { - destination: cache.declared.destination.to_owned(), + destination: cache.declared.destination.as_str().to_owned(), offset: cache.len, }; Ok(match read_chunk(credentials, session, &request).await? {