fix: cache/output dir migration (close #159) (#165)

* fix: cache/output dir migration (close #159)

* chore: adjust wrong log info

* fix: more accurate way to check path
This commit is contained in:
Xinrea
2025-08-20 22:15:46 +08:00
committed by GitHub
parent 3c76be9b81
commit 50ab608ddb
2 changed files with 61 additions and 14 deletions

View File

@@ -14,10 +14,27 @@ pub async fn get_config(state: state_type!()) -> Result<Config, ()> {
#[allow(dead_code)]
pub async fn set_cache_path(state: state_type!(), cache_path: String) -> Result<(), String> {
let old_cache_path = state.config.read().await.cache.clone();
log::info!(
"Try to set cache path: {} -> {}",
old_cache_path,
cache_path
);
if old_cache_path == cache_path {
return Ok(());
}
let old_cache_path_obj = std::path::Path::new(&old_cache_path);
let new_cache_path_obj = std::path::Path::new(&cache_path);
// check if new cache path is under old cache path
if new_cache_path_obj.starts_with(old_cache_path_obj) {
log::error!(
"New cache path is under old cache path: {} -> {}",
old_cache_path,
cache_path
);
return Err("New cache path cannot be under old cache path".to_string());
}
state.recorder_manager.set_migrating(true).await;
// stop and clear all recorders
state.recorder_manager.stop_all().await;
@@ -52,9 +69,11 @@ pub async fn set_cache_path(state: state_type!(), cache_path: String) -> Result<
if entry.is_dir() {
if let Err(e) = crate::handlers::utils::copy_dir_all(entry, &new_entry) {
log::error!("Copy old cache to new cache error: {}", e);
return Err(e.to_string());
}
} else if let Err(e) = std::fs::copy(entry, &new_entry) {
log::error!("Copy old cache to new cache error: {}", e);
return Err(e.to_string());
}
}
@@ -79,12 +98,30 @@ pub async fn set_cache_path(state: state_type!(), cache_path: String) -> Result<
#[cfg_attr(feature = "gui", tauri::command)]
#[allow(dead_code)]
pub async fn set_output_path(state: state_type!(), output_path: String) -> Result<(), ()> {
pub async fn set_output_path(state: state_type!(), output_path: String) -> Result<(), String> {
let mut config = state.config.write().await;
let old_output_path = config.output.clone();
log::info!(
"Try to set output path: {} -> {}",
old_output_path,
output_path
);
if old_output_path == output_path {
return Ok(());
}
let old_output_path_obj = std::path::Path::new(&old_output_path);
let new_output_path_obj = std::path::Path::new(&output_path);
// check if new output path is under old output path
if new_output_path_obj.starts_with(old_output_path_obj) {
log::error!(
"New output path is under old output path: {} -> {}",
old_output_path,
output_path
);
return Err("New output path cannot be under old output path".to_string());
}
// list all file and folder in old output
let mut old_output_entries = vec![];
if let Ok(entries) = std::fs::read_dir(&old_output_path) {
@@ -103,10 +140,12 @@ pub async fn set_output_path(state: state_type!(), output_path: String) -> Resul
// if entry is a folder
if entry.is_dir() {
if let Err(e) = crate::handlers::utils::copy_dir_all(entry, &new_entry) {
log::error!("Copy old cache to new cache error: {}", e);
log::error!("Copy old output to new output error: {}", e);
return Err(e.to_string());
}
} else if let Err(e) = std::fs::copy(entry, &new_entry) {
log::error!("Copy old cache to new cache error: {}", e);
log::error!("Copy old output to new output error: {}", e);
return Err(e.to_string());
}
}
@@ -114,10 +153,10 @@ pub async fn set_output_path(state: state_type!(), output_path: String) -> Resul
for entry in old_output_entries {
if entry.is_dir() {
if let Err(e) = std::fs::remove_dir_all(&entry) {
log::error!("Remove old cache error: {}", e);
log::error!("Remove old output error: {}", e);
}
} else if let Err(e) = std::fs::remove_file(&entry) {
log::error!("Remove old cache error: {}", e);
log::error!("Remove old output error: {}", e);
}
}
@@ -251,4 +290,4 @@ pub async fn update_user_agent(state: state_type!(), user_agent: String) -> Resu
log::info!("Updating user agent to {}", user_agent);
state.config.write().await.set_user_agent(&user_agent);
Ok(())
}
}

View File

@@ -77,10 +77,14 @@
async function handleOutputChange() {
const new_folder = await browse_folder();
if (new_folder) {
setting_model.output = new_folder;
await invoke("set_output_path", {
outputPath: setting_model.output,
});
try {
await invoke("set_output_path", {
outputPath: new_folder,
});
setting_model.output = new_folder;
} catch (e) {
alert(e);
}
}
}
@@ -92,10 +96,14 @@
showModal = false;
const new_folder = await browse_folder();
if (new_folder) {
setting_model.cache = new_folder;
await invoke("set_cache_path", {
cachePath: setting_model.cache,
});
try {
await invoke("set_cache_path", {
cachePath: new_folder,
});
setting_model.cache = new_folder;
} catch (e) {
alert(e);
}
}
}